PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - sqlite/libsqlite/src - expr.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 788
Code covered: 38.2 % Executed lines: 301
Legend: not executed executed

       1                 : /*
       2                 : ** 2001 September 15
       3                 : **
       4                 : ** The author disclaims copyright to this source code.  In place of
       5                 : ** a legal notice, here is a blessing:
       6                 : **
       7                 : **    May you do good and not evil.
       8                 : **    May you find forgiveness for yourself and forgive others.
       9                 : **    May you share freely, never taking more than you give.
      10                 : **
      11                 : *************************************************************************
      12                 : ** This file contains routines used for analyzing expressions and
      13                 : ** for generating VDBE code that evaluates expressions in SQLite.
      14                 : **
      15                 : ** $Id: expr.c 195361 2005-09-07 15:11:33Z iliaa $
      16                 : */
      17                 : #include "sqliteInt.h"
      18                 : #include <ctype.h>
      19                 : 
      20                 : /*
      21                 : ** Construct a new expression node and return a pointer to it.  Memory
      22                 : ** for this node is obtained from sqliteMalloc().  The calling function
      23                 : ** is responsible for making sure the node eventually gets freed.
      24                 : */
      25            2866 : Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
      26                 :   Expr *pNew;
      27            2866 :   pNew = sqliteMalloc( sizeof(Expr) );
      28            2866 :   if( pNew==0 ){
      29                 :     /* When malloc fails, we leak memory from pLeft and pRight */
      30               0 :     return 0;
      31                 :   }
      32            2866 :   pNew->op = op;
      33            2866 :   pNew->pLeft = pLeft;
      34            2866 :   pNew->pRight = pRight;
      35            2866 :   if( pToken ){
      36                 :     assert( pToken->dyn==0 );
      37            2483 :     pNew->token = *pToken;
      38            2483 :     pNew->span = *pToken;
      39                 :   }else{
      40                 :     assert( pNew->token.dyn==0 );
      41                 :     assert( pNew->token.z==0 );
      42                 :     assert( pNew->token.n==0 );
      43             518 :     if( pLeft && pRight ){
      44             135 :       sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
      45                 :     }else{
      46             248 :       pNew->span = pNew->token;
      47                 :     }
      48                 :   }
      49            2866 :   return pNew;
      50                 : }
      51                 : 
      52                 : /*
      53                 : ** Set the Expr.span field of the given expression to span all
      54                 : ** text between the two given tokens.
      55                 : */
      56             164 : void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
      57                 :   assert( pRight!=0 );
      58                 :   assert( pLeft!=0 );
      59                 :   /* Note: pExpr might be NULL due to a prior malloc failure */
      60             164 :   if( pExpr && pRight->z && pLeft->z ){
      61             304 :     if( pLeft->dyn==0 && pRight->dyn==0 ){
      62             152 :       pExpr->span.z = pLeft->z;
      63             152 :       pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
      64                 :     }else{
      65               0 :       pExpr->span.z = 0;
      66                 :     }
      67                 :   }
      68             164 : }
      69                 : 
      70                 : /*
      71                 : ** Construct a new expression node for a function with multiple
      72                 : ** arguments.
      73                 : */
      74              23 : Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
      75                 :   Expr *pNew;
      76              23 :   pNew = sqliteMalloc( sizeof(Expr) );
      77              23 :   if( pNew==0 ){
      78                 :     /* sqliteExprListDelete(pList); // Leak pList when malloc fails */
      79               0 :     return 0;
      80                 :   }
      81              23 :   pNew->op = TK_FUNCTION;
      82              23 :   pNew->pList = pList;
      83              23 :   if( pToken ){
      84                 :     assert( pToken->dyn==0 );
      85              23 :     pNew->token = *pToken;
      86                 :   }else{
      87               0 :     pNew->token.z = 0;
      88                 :   }
      89              23 :   pNew->span = pNew->token;
      90              23 :   return pNew;
      91                 : }
      92                 : 
      93                 : /*
      94                 : ** Recursively delete an expression tree.
      95                 : */
      96           13546 : void sqliteExprDelete(Expr *p){
      97           13546 :   if( p==0 ) return;
      98            2889 :   if( p->span.dyn ) sqliteFree((char*)p->span.z);
      99            2889 :   if( p->token.dyn ) sqliteFree((char*)p->token.z);
     100            2889 :   sqliteExprDelete(p->pLeft);
     101            2889 :   sqliteExprDelete(p->pRight);
     102            2889 :   sqliteExprListDelete(p->pList);
     103            2889 :   sqliteSelectDelete(p->pSelect);
     104            2889 :   sqliteFree(p);
     105                 : }
     106                 : 
     107                 : 
     108                 : /*
     109                 : ** The following group of routines make deep copies of expressions,
     110                 : ** expression lists, ID lists, and select statements.  The copies can
     111                 : ** be deleted (by being passed to their respective ...Delete() routines)
     112                 : ** without effecting the originals.
     113                 : **
     114                 : ** The expression list, ID, and source lists return by sqliteExprListDup(),
     115                 : ** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded 
     116                 : ** by subsequent calls to sqlite*ListAppend() routines.
     117                 : **
     118                 : ** Any tables that the SrcList might point to are not duplicated.
     119                 : */
     120               0 : Expr *sqliteExprDup(Expr *p){
     121                 :   Expr *pNew;
     122               0 :   if( p==0 ) return 0;
     123               0 :   pNew = sqliteMallocRaw( sizeof(*p) );
     124               0 :   if( pNew==0 ) return 0;
     125               0 :   memcpy(pNew, p, sizeof(*pNew));
     126               0 :   if( p->token.z!=0 ){
     127               0 :     pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
     128               0 :     pNew->token.dyn = 1;
     129                 :   }else{
     130                 :     assert( pNew->token.z==0 );
     131                 :   }
     132               0 :   pNew->span.z = 0;
     133               0 :   pNew->pLeft = sqliteExprDup(p->pLeft);
     134               0 :   pNew->pRight = sqliteExprDup(p->pRight);
     135               0 :   pNew->pList = sqliteExprListDup(p->pList);
     136               0 :   pNew->pSelect = sqliteSelectDup(p->pSelect);
     137               0 :   return pNew;
     138                 : }
     139               0 : void sqliteTokenCopy(Token *pTo, Token *pFrom){
     140               0 :   if( pTo->dyn ) sqliteFree((char*)pTo->z);
     141               0 :   if( pFrom->z ){
     142               0 :     pTo->n = pFrom->n;
     143               0 :     pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
     144               0 :     pTo->dyn = 1;
     145                 :   }else{
     146               0 :     pTo->z = 0;
     147                 :   }
     148               0 : }
     149               0 : ExprList *sqliteExprListDup(ExprList *p){
     150                 :   ExprList *pNew;
     151                 :   struct ExprList_item *pItem;
     152                 :   int i;
     153               0 :   if( p==0 ) return 0;
     154               0 :   pNew = sqliteMalloc( sizeof(*pNew) );
     155               0 :   if( pNew==0 ) return 0;
     156               0 :   pNew->nExpr = pNew->nAlloc = p->nExpr;
     157               0 :   pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
     158               0 :   if( pItem==0 ){
     159               0 :     sqliteFree(pNew);
     160               0 :     return 0;
     161                 :   }
     162               0 :   for(i=0; i<p->nExpr; i++, pItem++){
     163                 :     Expr *pNewExpr, *pOldExpr;
     164               0 :     pItem->pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr);
     165               0 :     if( pOldExpr->span.z!=0 && pNewExpr ){
     166                 :       /* Always make a copy of the span for top-level expressions in the
     167                 :       ** expression list.  The logic in SELECT processing that determines
     168                 :       ** the names of columns in the result set needs this information */
     169               0 :       sqliteTokenCopy(&pNewExpr->span, &pOldExpr->span);
     170                 :     }
     171                 :     assert( pNewExpr==0 || pNewExpr->span.z!=0 
     172                 :             || pOldExpr->span.z==0 || sqlite_malloc_failed );
     173               0 :     pItem->zName = sqliteStrDup(p->a[i].zName);
     174               0 :     pItem->sortOrder = p->a[i].sortOrder;
     175               0 :     pItem->isAgg = p->a[i].isAgg;
     176               0 :     pItem->done = 0;
     177                 :   }
     178               0 :   return pNew;
     179                 : }
     180               0 : SrcList *sqliteSrcListDup(SrcList *p){
     181                 :   SrcList *pNew;
     182                 :   int i;
     183                 :   int nByte;
     184               0 :   if( p==0 ) return 0;
     185               0 :   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
     186               0 :   pNew = sqliteMallocRaw( nByte );
     187               0 :   if( pNew==0 ) return 0;
     188               0 :   pNew->nSrc = pNew->nAlloc = p->nSrc;
     189               0 :   for(i=0; i<p->nSrc; i++){
     190               0 :     struct SrcList_item *pNewItem = &pNew->a[i];
     191               0 :     struct SrcList_item *pOldItem = &p->a[i];
     192               0 :     pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
     193               0 :     pNewItem->zName = sqliteStrDup(pOldItem->zName);
     194               0 :     pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
     195               0 :     pNewItem->jointype = pOldItem->jointype;
     196               0 :     pNewItem->iCursor = pOldItem->iCursor;
     197               0 :     pNewItem->pTab = 0;
     198               0 :     pNewItem->pSelect = sqliteSelectDup(pOldItem->pSelect);
     199               0 :     pNewItem->pOn = sqliteExprDup(pOldItem->pOn);
     200               0 :     pNewItem->pUsing = sqliteIdListDup(pOldItem->pUsing);
     201                 :   }
     202               0 :   return pNew;
     203                 : }
     204               0 : IdList *sqliteIdListDup(IdList *p){
     205                 :   IdList *pNew;
     206                 :   int i;
     207               0 :   if( p==0 ) return 0;
     208               0 :   pNew = sqliteMallocRaw( sizeof(*pNew) );
     209               0 :   if( pNew==0 ) return 0;
     210               0 :   pNew->nId = pNew->nAlloc = p->nId;
     211               0 :   pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
     212               0 :   if( pNew->a==0 ) return 0;
     213               0 :   for(i=0; i<p->nId; i++){
     214               0 :     struct IdList_item *pNewItem = &pNew->a[i];
     215               0 :     struct IdList_item *pOldItem = &p->a[i];
     216               0 :     pNewItem->zName = sqliteStrDup(pOldItem->zName);
     217               0 :     pNewItem->idx = pOldItem->idx;
     218                 :   }
     219               0 :   return pNew;
     220                 : }
     221               0 : Select *sqliteSelectDup(Select *p){
     222                 :   Select *pNew;
     223               0 :   if( p==0 ) return 0;
     224               0 :   pNew = sqliteMallocRaw( sizeof(*p) );
     225               0 :   if( pNew==0 ) return 0;
     226               0 :   pNew->isDistinct = p->isDistinct;
     227               0 :   pNew->pEList = sqliteExprListDup(p->pEList);
     228               0 :   pNew->pSrc = sqliteSrcListDup(p->pSrc);
     229               0 :   pNew->pWhere = sqliteExprDup(p->pWhere);
     230               0 :   pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
     231               0 :   pNew->pHaving = sqliteExprDup(p->pHaving);
     232               0 :   pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
     233               0 :   pNew->op = p->op;
     234               0 :   pNew->pPrior = sqliteSelectDup(p->pPrior);
     235               0 :   pNew->nLimit = p->nLimit;
     236               0 :   pNew->nOffset = p->nOffset;
     237               0 :   pNew->zSelect = 0;
     238               0 :   pNew->iLimit = -1;
     239               0 :   pNew->iOffset = -1;
     240               0 :   return pNew;
     241                 : }
     242                 : 
     243                 : 
     244                 : /*
     245                 : ** Add a new element to the end of an expression list.  If pList is
     246                 : ** initially NULL, then create a new expression list.
     247                 : */
     248            2544 : ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
     249            2544 :   if( pList==0 ){
     250             874 :     pList = sqliteMalloc( sizeof(ExprList) );
     251             874 :     if( pList==0 ){
     252                 :       /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
     253               0 :       return 0;
     254                 :     }
     255                 :     assert( pList->nAlloc==0 );
     256                 :   }
     257            2544 :   if( pList->nAlloc<=pList->nExpr ){
     258            1178 :     pList->nAlloc = pList->nAlloc*2 + 4;
     259            1178 :     pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
     260            1178 :     if( pList->a==0 ){
     261                 :       /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
     262               0 :       pList->nExpr = pList->nAlloc = 0;
     263               0 :       return pList;
     264                 :     }
     265                 :   }
     266                 :   assert( pList->a!=0 );
     267            2544 :   if( pExpr || pName ){
     268            2544 :     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
     269            2544 :     memset(pItem, 0, sizeof(*pItem));
     270            2544 :     pItem->pExpr = pExpr;
     271            2544 :     if( pName ){
     272              22 :       sqliteSetNString(&pItem->zName, pName->z, pName->n, 0);
     273              22 :       sqliteDequote(pItem->zName);
     274                 :     }
     275                 :   }
     276            2544 :   return pList;
     277                 : }
     278                 : 
     279                 : /*
     280                 : ** Delete an entire expression list.
     281                 : */
     282            4790 : void sqliteExprListDelete(ExprList *pList){
     283                 :   int i;
     284            4790 :   if( pList==0 ) return;
     285                 :   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
     286                 :   assert( pList->nExpr<=pList->nAlloc );
     287            3418 :   for(i=0; i<pList->nExpr; i++){
     288            2544 :     sqliteExprDelete(pList->a[i].pExpr);
     289            2544 :     sqliteFree(pList->a[i].zName);
     290                 :   }
     291             874 :   sqliteFree(pList->a);
     292             874 :   sqliteFree(pList);
     293                 : }
     294                 : 
     295                 : /*
     296                 : ** Walk an expression tree.  Return 1 if the expression is constant
     297                 : ** and 0 if it involves variables.
     298                 : **
     299                 : ** For the purposes of this function, a double-quoted string (ex: "abc")
     300                 : ** is considered a variable but a single-quoted string (ex: 'abc') is
     301                 : ** a constant.
     302                 : */
     303             154 : int sqliteExprIsConstant(Expr *p){
     304             154 :   switch( p->op ){
     305                 :     case TK_ID:
     306                 :     case TK_COLUMN:
     307                 :     case TK_DOT:
     308                 :     case TK_FUNCTION:
     309              78 :       return 0;
     310                 :     case TK_NULL:
     311                 :     case TK_STRING:
     312                 :     case TK_INTEGER:
     313                 :     case TK_FLOAT:
     314                 :     case TK_VARIABLE:
     315               1 :       return 1;
     316                 :     default: {
     317              75 :       if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
     318               0 :       if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
     319               0 :       if( p->pList ){
     320                 :         int i;
     321               0 :         for(i=0; i<p->pList->nExpr; i++){
     322               0 :           if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
     323                 :         }
     324                 :       }
     325               0 :       return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
     326                 :     }
     327                 :   }
     328                 :   return 0;
     329                 : }
     330                 : 
     331                 : /*
     332                 : ** If the given expression codes a constant integer that is small enough
     333                 : ** to fit in a 32-bit integer, return 1 and put the value of the integer
     334                 : ** in *pValue.  If the expression is not an integer or if it is too big
     335                 : ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
     336                 : */
     337               9 : int sqliteExprIsInteger(Expr *p, int *pValue){
     338               9 :   switch( p->op ){
     339                 :     case TK_INTEGER: {
     340               0 :       if( sqliteFitsIn32Bits(p->token.z) ){
     341               0 :         *pValue = atoi(p->token.z);
     342               0 :         return 1;
     343                 :       }
     344               0 :       break;
     345                 :     }
     346                 :     case TK_STRING: {
     347               0 :       const char *z = p->token.z;
     348               0 :       int n = p->token.n;
     349               0 :       if( n>0 && z[0]=='-' ){ z++; n--; }
     350               0 :       while( n>0 && *z && isdigit(*z) ){ z++; n--; }
     351               0 :       if( n==0 && sqliteFitsIn32Bits(p->token.z) ){
     352               0 :         *pValue = atoi(p->token.z);
     353               0 :         return 1;
     354                 :       }
     355               0 :       break;
     356                 :     }
     357                 :     case TK_UPLUS: {
     358               0 :       return sqliteExprIsInteger(p->pLeft, pValue);
     359                 :     }
     360                 :     case TK_UMINUS: {
     361                 :       int v;
     362               0 :       if( sqliteExprIsInteger(p->pLeft, &v) ){
     363               0 :         *pValue = -v;
     364               0 :         return 1;
     365                 :       }
     366                 :       break;
     367                 :     }
     368                 :     default: break;
     369                 :   }
     370               9 :   return 0;
     371                 : }
     372                 : 
     373                 : /*
     374                 : ** Return TRUE if the given string is a row-id column name.
     375                 : */
     376               0 : int sqliteIsRowid(const char *z){
     377               0 :   if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
     378               0 :   if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
     379               0 :   if( sqliteStrICmp(z, "OID")==0 ) return 1;
     380               0 :   return 0;
     381                 : }
     382                 : 
     383                 : /*
     384                 : ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
     385                 : ** that name in the set of source tables in pSrcList and make the pExpr 
     386                 : ** expression node refer back to that source column.  The following changes
     387                 : ** are made to pExpr:
     388                 : **
     389                 : **    pExpr->iDb           Set the index in db->aDb[] of the database holding
     390                 : **                         the table.
     391                 : **    pExpr->iTable        Set to the cursor number for the table obtained
     392                 : **                         from pSrcList.
     393                 : **    pExpr->iColumn       Set to the column number within the table.
     394                 : **    pExpr->dataType      Set to the appropriate data type for the column.
     395                 : **    pExpr->op            Set to TK_COLUMN.
     396                 : **    pExpr->pLeft         Any expression this points to is deleted
     397                 : **    pExpr->pRight        Any expression this points to is deleted.
     398                 : **
     399                 : ** The pDbToken is the name of the database (the "X").  This value may be
     400                 : ** NULL meaning that name is of the form Y.Z or Z.  Any available database
     401                 : ** can be used.  The pTableToken is the name of the table (the "Y").  This
     402                 : ** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
     403                 : ** means that the form of the name is Z and that columns from any table
     404                 : ** can be used.
     405                 : **
     406                 : ** If the name cannot be resolved unambiguously, leave an error message
     407                 : ** in pParse and return non-zero.  Return zero on success.
     408                 : */
     409                 : static int lookupName(
     410                 :   Parse *pParse,      /* The parsing context */
     411                 :   Token *pDbToken,     /* Name of the database containing table, or NULL */
     412                 :   Token *pTableToken,  /* Name of table containing column, or NULL */
     413                 :   Token *pColumnToken, /* Name of the column. */
     414                 :   SrcList *pSrcList,   /* List of tables used to resolve column names */
     415                 :   ExprList *pEList,    /* List of expressions used to resolve "AS" */
     416                 :   Expr *pExpr          /* Make this EXPR node point to the selected column */
     417            1690 : ){
     418            1690 :   char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
     419            1690 :   char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
     420            1690 :   char *zCol = 0;      /* Name of the column.  The "Z" */
     421                 :   int i, j;            /* Loop counters */
     422            1690 :   int cnt = 0;         /* Number of matching column names */
     423            1690 :   int cntTab = 0;      /* Number of matching table names */
     424            1690 :   sqlite *db = pParse->db;  /* The database */
     425                 : 
     426                 :   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
     427            1690 :   if( pDbToken && pDbToken->z ){
     428               0 :     zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
     429               0 :     sqliteDequote(zDb);
     430                 :   }else{
     431            1690 :     zDb = 0;
     432                 :   }
     433            1745 :   if( pTableToken && pTableToken->z ){
     434              55 :     zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
     435              55 :     sqliteDequote(zTab);
     436                 :   }else{
     437                 :     assert( zDb==0 );
     438            1635 :     zTab = 0;
     439                 :   }
     440            1690 :   zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
     441            1690 :   sqliteDequote(zCol);
     442            1690 :   if( sqlite_malloc_failed ){
     443               0 :     return 1;  /* Leak memory (zDb and zTab) if malloc fails */
     444                 :   }
     445                 :   assert( zTab==0 || pEList==0 );
     446                 : 
     447            1690 :   pExpr->iTable = -1;
     448            3432 :   for(i=0; i<pSrcList->nSrc; i++){
     449            1742 :     struct SrcList_item *pItem = &pSrcList->a[i];
     450            1742 :     Table *pTab = pItem->pTab;
     451                 :     Column *pCol;
     452                 : 
     453            1742 :     if( pTab==0 ) continue;
     454                 :     assert( pTab->nCol>0 );
     455            1742 :     if( zTab ){
     456             109 :       if( pItem->zAlias ){
     457               0 :         char *zTabName = pItem->zAlias;
     458               0 :         if( sqliteStrICmp(zTabName, zTab)!=0 ) continue;
     459                 :       }else{
     460             109 :         char *zTabName = pTab->zName;
     461             109 :         if( zTabName==0 || sqliteStrICmp(zTabName, zTab)!=0 ) continue;
     462              55 :         if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
     463               0 :           continue;
     464                 :         }
     465                 :       }
     466                 :     }
     467            1688 :     if( 0==(cntTab++) ){
     468            1688 :       pExpr->iTable = pItem->iCursor;
     469            1688 :       pExpr->iDb = pTab->iDb;
     470                 :     }
     471            4424 :     for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
     472            4424 :       if( sqliteStrICmp(pCol->zName, zCol)==0 ){
     473            1688 :         cnt++;
     474            1688 :         pExpr->iTable = pItem->iCursor;
     475            1688 :         pExpr->iDb = pTab->iDb;
     476                 :         /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
     477            1688 :         pExpr->iColumn = j==pTab->iPKey ? -1 : j;
     478            1688 :         pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK;
     479            1688 :         break;
     480                 :       }
     481                 :     }
     482                 :   }
     483                 : 
     484                 :   /* If we have not already resolved the name, then maybe 
     485                 :   ** it is a new.* or old.* trigger argument reference
     486                 :   */
     487            1690 :   if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
     488               0 :     TriggerStack *pTriggerStack = pParse->trigStack;
     489               0 :     Table *pTab = 0;
     490               0 :     if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zTab) == 0 ){
     491               0 :       pExpr->iTable = pTriggerStack->newIdx;
     492                 :       assert( pTriggerStack->pTab );
     493               0 :       pTab = pTriggerStack->pTab;
     494               0 :     }else if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zTab) == 0 ){
     495               0 :       pExpr->iTable = pTriggerStack->oldIdx;
     496                 :       assert( pTriggerStack->pTab );
     497               0 :       pTab = pTriggerStack->pTab;
     498                 :     }
     499                 : 
     500               0 :     if( pTab ){ 
     501                 :       int j;
     502               0 :       Column *pCol = pTab->aCol;
     503                 :       
     504               0 :       pExpr->iDb = pTab->iDb;
     505               0 :       cntTab++;
     506               0 :       for(j=0; j < pTab->nCol; j++, pCol++) {
     507               0 :         if( sqliteStrICmp(pCol->zName, zCol)==0 ){
     508               0 :           cnt++;
     509               0 :           pExpr->iColumn = j==pTab->iPKey ? -1 : j;
     510               0 :           pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK;
     511               0 :           break;
     512                 :         }
     513                 :       }
     514                 :     }
     515                 :   }
     516                 : 
     517                 :   /*
     518                 :   ** Perhaps the name is a reference to the ROWID
     519                 :   */
     520            1690 :   if( cnt==0 && cntTab==1 && sqliteIsRowid(zCol) ){
     521               0 :     cnt = 1;
     522               0 :     pExpr->iColumn = -1;
     523               0 :     pExpr->dataType = SQLITE_SO_NUM;
     524                 :   }
     525                 : 
     526                 :   /*
     527                 :   ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
     528                 :   ** might refer to an result-set alias.  This happens, for example, when
     529                 :   ** we are resolving names in the WHERE clause of the following command:
     530                 :   **
     531                 :   **     SELECT a+b AS x FROM table WHERE x<10;
     532                 :   **
     533                 :   ** In cases like this, replace pExpr with a copy of the expression that
     534                 :   ** forms the result set entry ("a+b" in the example) and return immediately.
     535                 :   ** Note that the expression in the result set should have already been
     536                 :   ** resolved by the time the WHERE clause is resolved.
     537                 :   */
     538            1690 :   if( cnt==0 && pEList!=0 ){
     539               0 :     for(j=0; j<pEList->nExpr; j++){
     540               0 :       char *zAs = pEList->a[j].zName;
     541               0 :       if( zAs!=0 && sqliteStrICmp(zAs, zCol)==0 ){
     542                 :         assert( pExpr->pLeft==0 && pExpr->pRight==0 );
     543               0 :         pExpr->op = TK_AS;
     544               0 :         pExpr->iColumn = j;
     545               0 :         pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
     546               0 :         sqliteFree(zCol);
     547                 :         assert( zTab==0 && zDb==0 );
     548               0 :         return 0;
     549                 :       }
     550                 :     } 
     551                 :   }
     552                 : 
     553                 :   /*
     554                 :   ** If X and Y are NULL (in other words if only the column name Z is
     555                 :   ** supplied) and the value of Z is enclosed in double-quotes, then
     556                 :   ** Z is a string literal if it doesn't match any column names.  In that
     557                 :   ** case, we need to return right away and not make any changes to
     558                 :   ** pExpr.
     559                 :   */
     560            1690 :   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
     561               2 :     sqliteFree(zCol);
     562               2 :     return 0;
     563                 :   }
     564                 : 
     565                 :   /*
     566                 :   ** cnt==0 means there was not match.  cnt>1 means there were two or
     567                 :   ** more matches.  Either way, we have an error.
     568                 :   */
     569            1688 :   if( cnt!=1 ){
     570               0 :     char *z = 0;
     571                 :     char *zErr;
     572               0 :     zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
     573               0 :     if( zDb ){
     574               0 :       sqliteSetString(&z, zDb, ".", zTab, ".", zCol, 0);
     575               0 :     }else if( zTab ){
     576               0 :       sqliteSetString(&z, zTab, ".", zCol, 0);
     577                 :     }else{
     578               0 :       z = sqliteStrDup(zCol);
     579                 :     }
     580               0 :     sqliteErrorMsg(pParse, zErr, z);
     581               0 :     sqliteFree(z);
     582                 :   }
     583                 : 
     584                 :   /* Clean up and return
     585                 :   */
     586            1688 :   sqliteFree(zDb);
     587            1688 :   sqliteFree(zTab);
     588            1688 :   sqliteFree(zCol);
     589            1688 :   sqliteExprDelete(pExpr->pLeft);
     590            1688 :   pExpr->pLeft = 0;
     591            1688 :   sqliteExprDelete(pExpr->pRight);
     592            1688 :   pExpr->pRight = 0;
     593            1688 :   pExpr->op = TK_COLUMN;
     594            1688 :   sqliteAuthRead(pParse, pExpr, pSrcList);
     595            1688 :   return cnt!=1;
     596                 : }
     597                 : 
     598                 : /*
     599                 : ** This routine walks an expression tree and resolves references to
     600                 : ** table columns.  Nodes of the form ID.ID or ID resolve into an
     601                 : ** index to the table in the table list and a column offset.  The 
     602                 : ** Expr.opcode for such nodes is changed to TK_COLUMN.  The Expr.iTable
     603                 : ** value is changed to the index of the referenced table in pTabList
     604                 : ** plus the "base" value.  The base value will ultimately become the
     605                 : ** VDBE cursor number for a cursor that is pointing into the referenced
     606                 : ** table.  The Expr.iColumn value is changed to the index of the column 
     607                 : ** of the referenced table.  The Expr.iColumn value for the special
     608                 : ** ROWID column is -1.  Any INTEGER PRIMARY KEY column is tried as an
     609                 : ** alias for ROWID.
     610                 : **
     611                 : ** We also check for instances of the IN operator.  IN comes in two
     612                 : ** forms:
     613                 : **
     614                 : **           expr IN (exprlist)
     615                 : ** and
     616                 : **           expr IN (SELECT ...)
     617                 : **
     618                 : ** The first form is handled by creating a set holding the list
     619                 : ** of allowed values.  The second form causes the SELECT to generate 
     620                 : ** a temporary table.
     621                 : **
     622                 : ** This routine also looks for scalar SELECTs that are part of an expression.
     623                 : ** If it finds any, it generates code to write the value of that select
     624                 : ** into a memory cell.
     625                 : **
     626                 : ** Unknown columns or tables provoke an error.  The function returns
     627                 : ** the number of errors seen and leaves an error message on pParse->zErrMsg.
     628                 : */
     629                 : int sqliteExprResolveIds(
     630                 :   Parse *pParse,     /* The parser context */
     631                 :   SrcList *pSrcList, /* List of tables used to resolve column names */
     632                 :   ExprList *pEList,  /* List of expressions used to resolve "AS" */
     633                 :   Expr *pExpr        /* The expression to be analyzed. */
     634            2706 : ){
     635                 :   int i;
     636                 : 
     637            2706 :   if( pExpr==0 || pSrcList==0 ) return 0;
     638            2706 :   for(i=0; i<pSrcList->nSrc; i++){
     639                 :     assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
     640                 :   }
     641            2706 :   switch( pExpr->op ){
     642                 :     /* Double-quoted strings (ex: "abc") are used as identifiers if
     643                 :     ** possible.  Otherwise they remain as strings.  Single-quoted
     644                 :     ** strings (ex: 'abc') are always string literals.
     645                 :     */
     646                 :     case TK_STRING: {
     647             437 :       if( pExpr->token.z[0]=='\'' ) break;
     648                 :       /* Fall thru into the TK_ID case if this is a double-quoted string */
     649                 :     }
     650                 :     /* A lone identifier is the name of a columnd.
     651                 :     */
     652                 :     case TK_ID: {
     653            1635 :       if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
     654               0 :         return 1;
     655                 :       }
     656            1635 :       break; 
     657                 :     }
     658                 :   
     659                 :     /* A table name and column name:     ID.ID
     660                 :     ** Or a database, table and column:  ID.ID.ID
     661                 :     */
     662                 :     case TK_DOT: {
     663                 :       Token *pColumn;
     664                 :       Token *pTable;
     665                 :       Token *pDb;
     666                 :       Expr *pRight;
     667                 : 
     668              55 :       pRight = pExpr->pRight;
     669              55 :       if( pRight->op==TK_ID ){
     670              55 :         pDb = 0;
     671              55 :         pTable = &pExpr->pLeft->token;
     672              55 :         pColumn = &pRight->token;
     673                 :       }else{
     674                 :         assert( pRight->op==TK_DOT );
     675               0 :         pDb = &pExpr->pLeft->token;
     676               0 :         pTable = &pRight->pLeft->token;
     677               0 :         pColumn = &pRight->pRight->token;
     678                 :       }
     679              55 :       if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
     680               0 :         return 1;
     681                 :       }
     682              55 :       break;
     683                 :     }
     684                 : 
     685                 :     case TK_IN: {
     686               0 :       Vdbe *v = sqliteGetVdbe(pParse);
     687               0 :       if( v==0 ) return 1;
     688               0 :       if( sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
     689               0 :         return 1;
     690                 :       }
     691               0 :       if( pExpr->pSelect ){
     692                 :         /* Case 1:     expr IN (SELECT ...)
     693                 :         **
     694                 :         ** Generate code to write the results of the select into a temporary
     695                 :         ** table.  The cursor number of the temporary table has already
     696                 :         ** been put in iTable by sqliteExprResolveInSelect().
     697                 :         */
     698               0 :         pExpr->iTable = pParse->nTab++;
     699               0 :         sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
     700               0 :         sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
     701               0 :       }else if( pExpr->pList ){
     702                 :         /* Case 2:     expr IN (exprlist)
     703                 :         **
     704                 :         ** Create a set to put the exprlist values in.  The Set id is stored
     705                 :         ** in iTable.
     706                 :         */
     707                 :         int i, iSet;
     708               0 :         for(i=0; i<pExpr->pList->nExpr; i++){
     709               0 :           Expr *pE2 = pExpr->pList->a[i].pExpr;
     710               0 :           if( !sqliteExprIsConstant(pE2) ){
     711               0 :             sqliteErrorMsg(pParse,
     712                 :               "right-hand side of IN operator must be constant");
     713               0 :             return 1;
     714                 :           }
     715               0 :           if( sqliteExprCheck(pParse, pE2, 0, 0) ){
     716               0 :             return 1;
     717                 :           }
     718                 :         }
     719               0 :         iSet = pExpr->iTable = pParse->nSet++;
     720               0 :         for(i=0; i<pExpr->pList->nExpr; i++){
     721               0 :           Expr *pE2 = pExpr->pList->a[i].pExpr;
     722               0 :           switch( pE2->op ){
     723                 :             case TK_FLOAT:
     724                 :             case TK_INTEGER:
     725                 :             case TK_STRING: {
     726                 :               int addr;
     727                 :               assert( pE2->token.z );
     728               0 :               addr = sqliteVdbeOp3(v, OP_SetInsert, iSet, 0,
     729                 :                                   pE2->token.z, pE2->token.n);
     730               0 :               sqliteVdbeDequoteP3(v, addr);
     731               0 :               break;
     732                 :             }
     733                 :             default: {
     734               0 :               sqliteExprCode(pParse, pE2);
     735               0 :               sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
     736                 :               break;
     737                 :             }
     738                 :           }
     739                 :         }
     740                 :       }
     741               0 :       break;
     742                 :     }
     743                 : 
     744                 :     case TK_SELECT: {
     745                 :       /* This has to be a scalar SELECT.  Generate code to put the
     746                 :       ** value of this select in a memory cell and record the number
     747                 :       ** of the memory cell in iColumn.
     748                 :       */
     749               0 :       pExpr->iColumn = pParse->nMem++;
     750               0 :       if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
     751               0 :         return 1;
     752                 :       }
     753               0 :       break;
     754                 :     }
     755                 : 
     756                 :     /* For all else, just recursively walk the tree */
     757                 :     default: {
     758             581 :       if( pExpr->pLeft
     759                 :       && sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
     760               0 :         return 1;
     761                 :       }
     762             581 :       if( pExpr->pRight 
     763                 :       && sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
     764               0 :         return 1;
     765                 :       }
     766             581 :       if( pExpr->pList ){
     767                 :         int i;
     768              14 :         ExprList *pList = pExpr->pList;
     769              34 :         for(i=0; i<pList->nExpr; i++){
     770              20 :           Expr *pArg = pList->a[i].pExpr;
     771              20 :           if( sqliteExprResolveIds(pParse, pSrcList, pEList, pArg) ){
     772               0 :             return 1;
     773                 :           }
     774                 :         }
     775                 :       }
     776                 :     }
     777                 :   }
     778            2706 :   return 0;
     779                 : }
     780                 : 
     781                 : /*
     782                 : ** pExpr is a node that defines a function of some kind.  It might
     783                 : ** be a syntactic function like "count(x)" or it might be a function
     784                 : ** that implements an operator, like "a LIKE b".  
     785                 : **
     786                 : ** This routine makes *pzName point to the name of the function and 
     787                 : ** *pnName hold the number of characters in the function name.
     788                 : */
     789              32 : static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
     790              32 :   switch( pExpr->op ){
     791                 :     case TK_FUNCTION: {
     792              32 :       *pzName = pExpr->token.z;
     793              32 :       *pnName = pExpr->token.n;
     794              32 :       break;
     795                 :     }
     796                 :     case TK_LIKE: {
     797               0 :       *pzName = "like";
     798               0 :       *pnName = 4;
     799               0 :       break;
     800                 :     }
     801                 :     case TK_GLOB: {
     802               0 :       *pzName = "glob";
     803               0 :       *pnName = 4;
     804               0 :       break;
     805                 :     }
     806                 :     default: {
     807               0 :       *pzName = "can't happen";
     808               0 :       *pnName = 12;
     809                 :       break;
     810                 :     }
     811                 :   }
     812              32 : }
     813                 : 
     814                 : /*
     815                 : ** Error check the functions in an expression.  Make sure all
     816                 : ** function names are recognized and all functions have the correct
     817                 : ** number of arguments.  Leave an error message in pParse->zErrMsg
     818                 : ** if anything is amiss.  Return the number of errors.
     819                 : **
     820                 : ** if pIsAgg is not null and this expression is an aggregate function
     821                 : ** (like count(*) or max(value)) then write a 1 into *pIsAgg.
     822                 : */
     823            2726 : int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
     824            2726 :   int nErr = 0;
     825            2726 :   if( pExpr==0 ) return 0;
     826            2726 :   switch( pExpr->op ){
     827                 :     case TK_GLOB:
     828                 :     case TK_LIKE:
     829                 :     case TK_FUNCTION: {
     830              23 :       int n = pExpr->pList ? pExpr->pList->nExpr : 0;  /* Number of arguments */
     831              23 :       int no_such_func = 0;       /* True if no such function exists */
     832              23 :       int wrong_num_args = 0;     /* True if wrong number of arguments */
     833              23 :       int is_agg = 0;             /* True if is an aggregate function */
     834                 :       int i;
     835                 :       int nId;                    /* Number of characters in function name */
     836                 :       const char *zId;            /* The function name. */
     837                 :       FuncDef *pDef;
     838                 : 
     839              23 :       getFunctionName(pExpr, &zId, &nId);
     840              23 :       pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
     841              23 :       if( pDef==0 ){
     842               0 :         pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
     843               0 :         if( pDef==0 ){
     844               0 :           no_such_func = 1;
     845                 :         }else{
     846               0 :           wrong_num_args = 1;
     847                 :         }
     848                 :       }else{
     849              23 :         is_agg = pDef->xFunc==0;
     850                 :       }
     851              23 :       if( is_agg && !allowAgg ){
     852               0 :         sqliteErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
     853               0 :         nErr++;
     854               0 :         is_agg = 0;
     855              23 :       }else if( no_such_func ){
     856               0 :         sqliteErrorMsg(pParse, "no such function: %.*s", nId, zId);
     857               0 :         nErr++;
     858              23 :       }else if( wrong_num_args ){
     859               0 :         sqliteErrorMsg(pParse,"wrong number of arguments to function %.*s()",
     860                 :              nId, zId);
     861               0 :         nErr++;
     862                 :       }
     863              23 :       if( is_agg ){
     864              14 :         pExpr->op = TK_AGG_FUNCTION;
     865              14 :         if( pIsAgg ) *pIsAgg = 1;
     866                 :       }
     867              43 :       for(i=0; nErr==0 && i<n; i++){
     868              20 :         nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
     869                 :                                allowAgg && !is_agg, pIsAgg);
     870                 :       }
     871              23 :       if( pDef==0 ){
     872                 :         /* Already reported an error */
     873              23 :       }else if( pDef->dataType>=0 ){
     874               0 :         if( pDef->dataType<n ){
     875               0 :           pExpr->dataType = 
     876                 :              sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
     877                 :         }else{
     878               0 :           pExpr->dataType = SQLITE_SO_NUM;
     879                 :         }
     880              23 :       }else if( pDef->dataType==SQLITE_ARGS ){
     881               0 :         pDef->dataType = SQLITE_SO_TEXT;
     882               0 :         for(i=0; i<n; i++){
     883               0 :           if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
     884               0 :             pExpr->dataType = SQLITE_SO_NUM;
     885               0 :             break;
     886                 :           }
     887                 :         }
     888              23 :       }else if( pDef->dataType==SQLITE_NUMERIC ){
     889              23 :         pExpr->dataType = SQLITE_SO_NUM;
     890                 :       }else{
     891               0 :         pExpr->dataType = SQLITE_SO_TEXT;
     892                 :       }
     893                 :     }
     894                 :     default: {
     895            2726 :       if( pExpr->pLeft ){
     896              81 :         nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
     897                 :       }
     898            2726 :       if( nErr==0 && pExpr->pRight ){
     899              78 :         nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
     900                 :       }
     901            2726 :       if( nErr==0 && pExpr->pList ){
     902              14 :         int n = pExpr->pList->nExpr;
     903                 :         int i;
     904              34 :         for(i=0; nErr==0 && i<n; i++){
     905              20 :           Expr *pE2 = pExpr->pList->a[i].pExpr;
     906              20 :           nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
     907                 :         }
     908                 :       }
     909                 :       break;
     910                 :     }
     911                 :   }
     912            2726 :   return nErr;
     913                 : }
     914                 : 
     915                 : /*
     916                 : ** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
     917                 : ** given expression should sort as numeric values or as text.
     918                 : **
     919                 : ** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
     920                 : ** both been called on the expression before it is passed to this routine.
     921                 : */
     922             367 : int sqliteExprType(Expr *p){
     923             367 :   if( p==0 ) return SQLITE_SO_NUM;
     924             748 :   while( p ) switch( p->op ){
     925                 :     case TK_PLUS:
     926                 :     case TK_MINUS:
     927                 :     case TK_STAR:
     928                 :     case TK_SLASH:
     929                 :     case TK_AND:
     930                 :     case TK_OR:
     931                 :     case TK_ISNULL:
     932                 :     case TK_NOTNULL:
     933                 :     case TK_NOT:
     934                 :     case TK_UMINUS:
     935                 :     case TK_UPLUS:
     936                 :     case TK_BITAND:
     937                 :     case TK_BITOR:
     938                 :     case TK_BITNOT:
     939                 :     case TK_LSHIFT:
     940                 :     case TK_RSHIFT:
     941                 :     case TK_REM:
     942                 :     case TK_INTEGER:
     943                 :     case TK_FLOAT:
     944                 :     case TK_IN:
     945                 :     case TK_BETWEEN:
     946                 :     case TK_GLOB:
     947                 :     case TK_LIKE:
     948             305 :       return SQLITE_SO_NUM;
     949                 : 
     950                 :     case TK_STRING:
     951                 :     case TK_NULL:
     952                 :     case TK_CONCAT:
     953                 :     case TK_VARIABLE:
     954              15 :       return SQLITE_SO_TEXT;
     955                 : 
     956                 :     case TK_LT:
     957                 :     case TK_LE:
     958                 :     case TK_GT:
     959                 :     case TK_GE:
     960                 :     case TK_NE:
     961                 :     case TK_EQ:
     962              19 :       if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
     963               5 :         return SQLITE_SO_NUM;
     964                 :       }
     965              14 :       p = p->pRight;
     966              14 :       break;
     967                 : 
     968                 :     case TK_AS:
     969               0 :       p = p->pLeft;
     970               0 :       break;
     971                 : 
     972                 :     case TK_COLUMN:
     973                 :     case TK_FUNCTION:
     974                 :     case TK_AGG_FUNCTION:
     975              42 :       return p->dataType;
     976                 : 
     977                 :     case TK_SELECT:
     978                 :       assert( p->pSelect );
     979                 :       assert( p->pSelect->pEList );
     980                 :       assert( p->pSelect->pEList->nExpr>0 );
     981               0 :       p = p->pSelect->pEList->a[0].pExpr;
     982               0 :       break;
     983                 : 
     984                 :     case TK_CASE: {
     985               0 :       if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
     986               0 :         return SQLITE_SO_NUM;
     987                 :       }
     988               0 :       if( p->pList ){
     989                 :         int i;
     990               0 :         ExprList *pList = p->pList;
     991               0 :         for(i=1; i<pList->nExpr; i+=2){
     992               0 :           if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
     993               0 :             return SQLITE_SO_NUM;
     994                 :           }
     995                 :         }
     996                 :       }
     997               0 :       return SQLITE_SO_TEXT;
     998                 :     }
     999                 : 
    1000                 :     default:
    1001                 :       assert( p->op==TK_ABORT );  /* Can't Happen */
    1002                 :       break;
    1003                 :   }
    1004               0 :   return SQLITE_SO_NUM;
    1005                 : }
    1006                 : 
    1007                 : /*
    1008                 : ** Generate code into the current Vdbe to evaluate the given
    1009                 : ** expression and leave the result on the top of stack.
    1010                 : */
    1011            2564 : void sqliteExprCode(Parse *pParse, Expr *pExpr){
    1012            2564 :   Vdbe *v = pParse->pVdbe;
    1013                 :   int op;
    1014            2564 :   if( v==0 || pExpr==0 ) return;
    1015            2564 :   switch( pExpr->op ){
    1016               0 :     case TK_PLUS:     op = OP_Add;      break;
    1017               0 :     case TK_MINUS:    op = OP_Subtract; break;
    1018               0 :     case TK_STAR:     op = OP_Multiply; break;
    1019               0 :     case TK_SLASH:    op = OP_Divide;   break;
    1020               0 :     case TK_AND:      op = OP_And;      break;
    1021               0 :     case TK_OR:       op = OP_Or;       break;
    1022               0 :     case TK_LT:       op = OP_Lt;       break;
    1023               0 :     case TK_LE:       op = OP_Le;       break;
    1024               0 :     case TK_GT:       op = OP_Gt;       break;
    1025               0 :     case TK_GE:       op = OP_Ge;       break;
    1026               0 :     case TK_NE:       op = OP_Ne;       break;
    1027               0 :     case TK_EQ:       op = OP_Eq;       break;
    1028               0 :     case TK_ISNULL:   op = OP_IsNull;   break;
    1029               0 :     case TK_NOTNULL:  op = OP_NotNull;  break;
    1030               0 :     case TK_NOT:      op = OP_Not;      break;
    1031               0 :     case TK_UMINUS:   op = OP_Negative; break;
    1032               0 :     case TK_BITAND:   op = OP_BitAnd;   break;
    1033               0 :     case TK_BITOR:    op = OP_BitOr;    break;
    1034               0 :     case TK_BITNOT:   op = OP_BitNot;   break;
    1035               0 :     case TK_LSHIFT:   op = OP_ShiftLeft;  break;
    1036               0 :     case TK_RSHIFT:   op = OP_ShiftRight; break;
    1037               0 :     case TK_REM:      op = OP_Remainder;  break;
    1038                 :     default: break;
    1039                 :   }
    1040            2564 :   switch( pExpr->op ){
    1041                 :     case TK_COLUMN: {
    1042            1626 :       if( pParse->useAgg ){
    1043               0 :         sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
    1044            1626 :       }else if( pExpr->iColumn>=0 ){
    1045            1612 :         sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
    1046                 :       }else{
    1047              14 :         sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
    1048                 :       }
    1049            1626 :       break;
    1050                 :     }
    1051                 :     case TK_STRING:
    1052                 :     case TK_FLOAT:
    1053                 :     case TK_INTEGER: {
    1054            1375 :       if( pExpr->op==TK_INTEGER && sqliteFitsIn32Bits(pExpr->token.z) ){
    1055             469 :         sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
    1056                 :       }else{
    1057             437 :         sqliteVdbeAddOp(v, OP_String, 0, 0);
    1058                 :       }
    1059                 :       assert( pExpr->token.z );
    1060             906 :       sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
    1061             906 :       sqliteVdbeDequoteP3(v, -1);
    1062             906 :       break;
    1063                 :     }
    1064                 :     case TK_NULL: {
    1065               9 :       sqliteVdbeAddOp(v, OP_String, 0, 0);
    1066               9 :       break;
    1067                 :     }
    1068                 :     case TK_VARIABLE: {
    1069               0 :       sqliteVdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
    1070               0 :       break;
    1071                 :     }
    1072                 :     case TK_LT:
    1073                 :     case TK_LE:
    1074                 :     case TK_GT:
    1075                 :     case TK_GE:
    1076                 :     case TK_NE:
    1077                 :     case TK_EQ: {
    1078               0 :       if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
    1079               0 :         op += 6;  /* Convert numeric opcodes to text opcodes */
    1080                 :       }
    1081                 :       /* Fall through into the next case */
    1082                 :     }
    1083                 :     case TK_AND:
    1084                 :     case TK_OR:
    1085                 :     case TK_PLUS:
    1086                 :     case TK_STAR:
    1087                 :     case TK_MINUS:
    1088                 :     case TK_REM:
    1089                 :     case TK_BITAND:
    1090                 :     case TK_BITOR:
    1091                 :     case TK_SLASH: {
    1092               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1093               0 :       sqliteExprCode(pParse, pExpr->pRight);
    1094               0 :       sqliteVdbeAddOp(v, op, 0, 0);
    1095               0 :       break;
    1096                 :     }
    1097                 :     case TK_LSHIFT:
    1098                 :     case TK_RSHIFT: {
    1099               0 :       sqliteExprCode(pParse, pExpr->pRight);
    1100               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1101               0 :       sqliteVdbeAddOp(v, op, 0, 0);
    1102               0 :       break;
    1103                 :     }
    1104                 :     case TK_CONCAT: {
    1105               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1106               0 :       sqliteExprCode(pParse, pExpr->pRight);
    1107               0 :       sqliteVdbeAddOp(v, OP_Concat, 2, 0);
    1108               0 :       break;
    1109                 :     }
    1110                 :     case TK_UMINUS: {
    1111                 :       assert( pExpr->pLeft );
    1112               0 :       if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
    1113               0 :         Token *p = &pExpr->pLeft->token;
    1114               0 :         char *z = sqliteMalloc( p->n + 2 );
    1115               0 :         sprintf(z, "-%.*s", p->n, p->z);
    1116               0 :         if( pExpr->pLeft->op==TK_INTEGER && sqliteFitsIn32Bits(z) ){
    1117               0 :           sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
    1118                 :         }else{
    1119               0 :           sqliteVdbeAddOp(v, OP_String, 0, 0);
    1120                 :         }
    1121               0 :         sqliteVdbeChangeP3(v, -1, z, p->n+1);
    1122               0 :         sqliteFree(z);
    1123               0 :         break;
    1124                 :       }
    1125                 :       /* Fall through into TK_NOT */
    1126                 :     }
    1127                 :     case TK_BITNOT:
    1128                 :     case TK_NOT: {
    1129               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1130               0 :       sqliteVdbeAddOp(v, op, 0, 0);
    1131               0 :       break;
    1132                 :     }
    1133                 :     case TK_ISNULL:
    1134                 :     case TK_NOTNULL: {
    1135                 :       int dest;
    1136               0 :       sqliteVdbeAddOp(v, OP_Integer, 1, 0);
    1137               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1138               0 :       dest = sqliteVdbeCurrentAddr(v) + 2;
    1139               0 :       sqliteVdbeAddOp(v, op, 1, dest);
    1140               0 :       sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
    1141               0 :       break;
    1142                 :     }
    1143                 :     case TK_AGG_FUNCTION: {
    1144              14 :       sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
    1145              14 :       break;
    1146                 :     }
    1147                 :     case TK_GLOB:
    1148                 :     case TK_LIKE:
    1149                 :     case TK_FUNCTION: {
    1150               9 :       ExprList *pList = pExpr->pList;
    1151               9 :       int nExpr = pList ? pList->nExpr : 0;
    1152                 :       FuncDef *pDef;
    1153                 :       int nId;
    1154                 :       const char *zId;
    1155               9 :       getFunctionName(pExpr, &zId, &nId);
    1156               9 :       pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
    1157                 :       assert( pDef!=0 );
    1158               9 :       nExpr = sqliteExprCodeExprList(pParse, pList, pDef->includeTypes);
    1159               9 :       sqliteVdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER);
    1160               9 :       break;
    1161                 :     }
    1162                 :     case TK_SELECT: {
    1163               0 :       sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
    1164               0 :       break;
    1165                 :     }
    1166                 :     case TK_IN: {
    1167                 :       int addr;
    1168               0 :       sqliteVdbeAddOp(v, OP_Integer, 1, 0);
    1169               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1170               0 :       addr = sqliteVdbeCurrentAddr(v);
    1171               0 :       sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
    1172               0 :       sqliteVdbeAddOp(v, OP_Pop, 2, 0);
    1173               0 :       sqliteVdbeAddOp(v, OP_String, 0, 0);
    1174               0 :       sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
    1175               0 :       if( pExpr->pSelect ){
    1176               0 :         sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
    1177                 :       }else{
    1178               0 :         sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
    1179                 :       }
    1180               0 :       sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
    1181               0 :       break;
    1182                 :     }
    1183                 :     case TK_BETWEEN: {
    1184               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1185               0 :       sqliteVdbeAddOp(v, OP_Dup, 0, 0);
    1186               0 :       sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
    1187               0 :       sqliteVdbeAddOp(v, OP_Ge, 0, 0);
    1188               0 :       sqliteVdbeAddOp(v, OP_Pull, 1, 0);
    1189               0 :       sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
    1190               0 :       sqliteVdbeAddOp(v, OP_Le, 0, 0);
    1191               0 :       sqliteVdbeAddOp(v, OP_And, 0, 0);
    1192               0 :       break;
    1193                 :     }
    1194                 :     case TK_UPLUS:
    1195                 :     case TK_AS: {
    1196               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1197               0 :       break;
    1198                 :     }
    1199                 :     case TK_CASE: {
    1200                 :       int expr_end_label;
    1201                 :       int jumpInst;
    1202                 :       int addr;
    1203                 :       int nExpr;
    1204                 :       int i;
    1205                 : 
    1206                 :       assert(pExpr->pList);
    1207                 :       assert((pExpr->pList->nExpr % 2) == 0);
    1208                 :       assert(pExpr->pList->nExpr > 0);
    1209               0 :       nExpr = pExpr->pList->nExpr;
    1210               0 :       expr_end_label = sqliteVdbeMakeLabel(v);
    1211               0 :       if( pExpr->pLeft ){
    1212               0 :         sqliteExprCode(pParse, pExpr->pLeft);
    1213                 :       }
    1214               0 :       for(i=0; i<nExpr; i=i+2){
    1215               0 :         sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
    1216               0 :         if( pExpr->pLeft ){
    1217               0 :           sqliteVdbeAddOp(v, OP_Dup, 1, 1);
    1218               0 :           jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
    1219               0 :           sqliteVdbeAddOp(v, OP_Pop, 1, 0);
    1220                 :         }else{
    1221               0 :           jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
    1222                 :         }
    1223               0 :         sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
    1224               0 :         sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
    1225               0 :         addr = sqliteVdbeCurrentAddr(v);
    1226               0 :         sqliteVdbeChangeP2(v, jumpInst, addr);
    1227                 :       }
    1228               0 :       if( pExpr->pLeft ){
    1229               0 :         sqliteVdbeAddOp(v, OP_Pop, 1, 0);
    1230                 :       }
    1231               0 :       if( pExpr->pRight ){
    1232               0 :         sqliteExprCode(pParse, pExpr->pRight);
    1233                 :       }else{
    1234               0 :         sqliteVdbeAddOp(v, OP_String, 0, 0);
    1235                 :       }
    1236               0 :       sqliteVdbeResolveLabel(v, expr_end_label);
    1237               0 :       break;
    1238                 :     }
    1239                 :     case TK_RAISE: {
    1240               0 :       if( !pParse->trigStack ){
    1241               0 :         sqliteErrorMsg(pParse,
    1242                 :                        "RAISE() may only be used within a trigger-program");
    1243               0 :         pParse->nErr++;
    1244               0 :         return;
    1245                 :       }
    1246               0 :       if( pExpr->iColumn == OE_Rollback ||
    1247                 :           pExpr->iColumn == OE_Abort ||
    1248                 :           pExpr->iColumn == OE_Fail ){
    1249               0 :           sqliteVdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
    1250                 :                            pExpr->token.z, pExpr->token.n);
    1251               0 :           sqliteVdbeDequoteP3(v, -1);
    1252                 :       } else {
    1253                 :           assert( pExpr->iColumn == OE_Ignore );
    1254               0 :           sqliteVdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
    1255                 :                            "(IGNORE jump)", 0);
    1256                 :       }
    1257                 :     }
    1258                 :     break;
    1259                 :   }
    1260                 : }
    1261                 : 
    1262                 : /*
    1263                 : ** Generate code that pushes the value of every element of the given
    1264                 : ** expression list onto the stack.  If the includeTypes flag is true,
    1265                 : ** then also push a string that is the datatype of each element onto
    1266                 : ** the stack after the value.
    1267                 : **
    1268                 : ** Return the number of elements pushed onto the stack.
    1269                 : */
    1270                 : int sqliteExprCodeExprList(
    1271                 :   Parse *pParse,     /* Parsing context */
    1272                 :   ExprList *pList,   /* The expression list to be coded */
    1273                 :   int includeTypes   /* TRUE to put datatypes on the stack too */
    1274              23 : ){
    1275                 :   struct ExprList_item *pItem;
    1276                 :   int i, n;
    1277                 :   Vdbe *v;
    1278              23 :   if( pList==0 ) return 0;
    1279              14 :   v = sqliteGetVdbe(pParse);
    1280              14 :   n = pList->nExpr;
    1281              34 :   for(pItem=pList->a, i=0; i<n; i++, pItem++){
    1282              20 :     sqliteExprCode(pParse, pItem->pExpr);
    1283              20 :     if( includeTypes ){
    1284               0 :       sqliteVdbeOp3(v, OP_String, 0, 0, 
    1285                 :          sqliteExprType(pItem->pExpr)==SQLITE_SO_NUM ? "numeric" : "text",
    1286                 :          P3_STATIC);
    1287                 :     }
    1288                 :   }
    1289              14 :   return includeTypes ? n*2 : n;
    1290                 : }
    1291                 : 
    1292                 : /*
    1293                 : ** Generate code for a boolean expression such that a jump is made
    1294                 : ** to the label "dest" if the expression is true but execution
    1295                 : ** continues straight thru if the expression is false.
    1296                 : **
    1297                 : ** If the expression evaluates to NULL (neither true nor false), then
    1298                 : ** take the jump if the jumpIfNull flag is true.
    1299                 : */
    1300               3 : void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
    1301               3 :   Vdbe *v = pParse->pVdbe;
    1302               3 :   int op = 0;
    1303               3 :   if( v==0 || pExpr==0 ) return;
    1304               3 :   switch( pExpr->op ){
    1305               0 :     case TK_LT:       op = OP_Lt;       break;
    1306               0 :     case TK_LE:       op = OP_Le;       break;
    1307               0 :     case TK_GT:       op = OP_Gt;       break;
    1308               0 :     case TK_GE:       op = OP_Ge;       break;
    1309               0 :     case TK_NE:       op = OP_Ne;       break;
    1310               0 :     case TK_EQ:       op = OP_Eq;       break;
    1311               3 :     case TK_ISNULL:   op = OP_IsNull;   break;
    1312               0 :     case TK_NOTNULL:  op = OP_NotNull;  break;
    1313                 :     default:  break;
    1314                 :   }
    1315               3 :   switch( pExpr->op ){
    1316                 :     case TK_AND: {
    1317               0 :       int d2 = sqliteVdbeMakeLabel(v);
    1318               0 :       sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
    1319               0 :       sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
    1320               0 :       sqliteVdbeResolveLabel(v, d2);
    1321               0 :       break;
    1322                 :     }
    1323                 :     case TK_OR: {
    1324               0 :       sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
    1325               0 :       sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
    1326               0 :       break;
    1327                 :     }
    1328                 :     case TK_NOT: {
    1329               0 :       sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
    1330               0 :       break;
    1331                 :     }
    1332                 :     case TK_LT:
    1333                 :     case TK_LE:
    1334                 :     case TK_GT:
    1335                 :     case TK_GE:
    1336                 :     case TK_NE:
    1337                 :     case TK_EQ: {
    1338               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1339               0 :       sqliteExprCode(pParse, pExpr->pRight);
    1340               0 :       if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
    1341               0 :         op += 6;  /* Convert numeric opcodes to text opcodes */
    1342                 :       }
    1343               0 :       sqliteVdbeAddOp(v, op, jumpIfNull, dest);
    1344               0 :       break;
    1345                 :     }
    1346                 :     case TK_ISNULL:
    1347                 :     case TK_NOTNULL: {
    1348               3 :       sqliteExprCode(pParse, pExpr->pLeft);
    1349               3 :       sqliteVdbeAddOp(v, op, 1, dest);
    1350               3 :       break;
    1351                 :     }
    1352                 :     case TK_IN: {
    1353                 :       int addr;
    1354               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1355               0 :       addr = sqliteVdbeCurrentAddr(v);
    1356               0 :       sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
    1357               0 :       sqliteVdbeAddOp(v, OP_Pop, 1, 0);
    1358               0 :       sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
    1359               0 :       if( pExpr->pSelect ){
    1360               0 :         sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
    1361                 :       }else{
    1362               0 :         sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
    1363                 :       }
    1364               0 :       break;
    1365                 :     }
    1366                 :     case TK_BETWEEN: {
    1367                 :       int addr;
    1368               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1369               0 :       sqliteVdbeAddOp(v, OP_Dup, 0, 0);
    1370               0 :       sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
    1371               0 :       addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
    1372               0 :       sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
    1373               0 :       sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
    1374               0 :       sqliteVdbeAddOp(v, OP_Integer, 0, 0);
    1375               0 :       sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
    1376               0 :       sqliteVdbeAddOp(v, OP_Pop, 1, 0);
    1377               0 :       break;
    1378                 :     }
    1379                 :     default: {
    1380               0 :       sqliteExprCode(pParse, pExpr);
    1381               0 :       sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
    1382                 :       break;
    1383                 :     }
    1384                 :   }
    1385                 : }
    1386                 : 
    1387                 : /*
    1388                 : ** Generate code for a boolean expression such that a jump is made
    1389                 : ** to the label "dest" if the expression is false but execution
    1390                 : ** continues straight thru if the expression is true.
    1391                 : **
    1392                 : ** If the expression evaluates to NULL (neither true nor false) then
    1393                 : ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
    1394                 : */
    1395              24 : void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
    1396              24 :   Vdbe *v = pParse->pVdbe;
    1397              24 :   int op = 0;
    1398              24 :   if( v==0 || pExpr==0 ) return;
    1399              24 :   switch( pExpr->op ){
    1400               0 :     case TK_LT:       op = OP_Ge;       break;
    1401               0 :     case TK_LE:       op = OP_Gt;       break;
    1402               3 :     case TK_GT:       op = OP_Le;       break;
    1403               0 :     case TK_GE:       op = OP_Lt;       break;
    1404               1 :     case TK_NE:       op = OP_Eq;       break;
    1405              15 :     case TK_EQ:       op = OP_Ne;       break;
    1406               0 :     case TK_ISNULL:   op = OP_NotNull;  break;
    1407               0 :     case TK_NOTNULL:  op = OP_IsNull;   break;
    1408                 :     default:  break;
    1409                 :   }
    1410              24 :   switch( pExpr->op ){
    1411                 :     case TK_AND: {
    1412               0 :       sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
    1413               0 :       sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
    1414               0 :       break;
    1415                 :     }
    1416                 :     case TK_OR: {
    1417               3 :       int d2 = sqliteVdbeMakeLabel(v);
    1418               3 :       sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
    1419               3 :       sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
    1420               3 :       sqliteVdbeResolveLabel(v, d2);
    1421               3 :       break;
    1422                 :     }
    1423                 :     case TK_NOT: {
    1424               0 :       sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
    1425               0 :       break;
    1426                 :     }
    1427                 :     case TK_LT:
    1428                 :     case TK_LE:
    1429                 :     case TK_GT:
    1430                 :     case TK_GE:
    1431                 :     case TK_NE:
    1432                 :     case TK_EQ: {
    1433              19 :       if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
    1434                 :         /* Convert numeric comparison opcodes into text comparison opcodes.
    1435                 :         ** This step depends on the fact that the text comparision opcodes are
    1436                 :         ** always 6 greater than their corresponding numeric comparison
    1437                 :         ** opcodes.
    1438                 :         */
    1439                 :         assert( OP_Eq+6 == OP_StrEq );
    1440              14 :         op += 6;
    1441                 :       }
    1442              19 :       sqliteExprCode(pParse, pExpr->pLeft);
    1443              19 :       sqliteExprCode(pParse, pExpr->pRight);
    1444              19 :       sqliteVdbeAddOp(v, op, jumpIfNull, dest);
    1445              19 :       break;
    1446                 :     }
    1447                 :     case TK_ISNULL:
    1448                 :     case TK_NOTNULL: {
    1449               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1450               0 :       sqliteVdbeAddOp(v, op, 1, dest);
    1451               0 :       break;
    1452                 :     }
    1453                 :     case TK_IN: {
    1454                 :       int addr;
    1455               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1456               0 :       addr = sqliteVdbeCurrentAddr(v);
    1457               0 :       sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
    1458               0 :       sqliteVdbeAddOp(v, OP_Pop, 1, 0);
    1459               0 :       sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
    1460               0 :       if( pExpr->pSelect ){
    1461               0 :         sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
    1462                 :       }else{
    1463               0 :         sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
    1464                 :       }
    1465               0 :       break;
    1466                 :     }
    1467                 :     case TK_BETWEEN: {
    1468                 :       int addr;
    1469               0 :       sqliteExprCode(pParse, pExpr->pLeft);
    1470               0 :       sqliteVdbeAddOp(v, OP_Dup, 0, 0);
    1471               0 :       sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
    1472               0 :       addr = sqliteVdbeCurrentAddr(v);
    1473               0 :       sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
    1474               0 :       sqliteVdbeAddOp(v, OP_Pop, 1, 0);
    1475               0 :       sqliteVdbeAddOp(v, OP_Goto, 0, dest);
    1476               0 :       sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
    1477               0 :       sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
    1478               0 :       break;
    1479                 :     }
    1480                 :     default: {
    1481               2 :       sqliteExprCode(pParse, pExpr);
    1482               2 :       sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
    1483                 :       break;
    1484                 :     }
    1485                 :   }
    1486                 : }
    1487                 : 
    1488                 : /*
    1489                 : ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
    1490                 : ** if they are identical and return FALSE if they differ in any way.
    1491                 : */
    1492               0 : int sqliteExprCompare(Expr *pA, Expr *pB){
    1493                 :   int i;
    1494               0 :   if( pA==0 ){
    1495               0 :     return pB==0;
    1496               0 :   }else if( pB==0 ){
    1497               0 :     return 0;
    1498                 :   }
    1499               0 :   if( pA->op!=pB->op ) return 0;
    1500               0 :   if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
    1501               0 :   if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
    1502               0 :   if( pA->pList ){
    1503               0 :     if( pB->pList==0 ) return 0;
    1504               0 :     if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
    1505               0 :     for(i=0; i<pA->pList->nExpr; i++){
    1506               0 :       if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
    1507               0 :         return 0;
    1508                 :       }
    1509                 :     }
    1510               0 :   }else if( pB->pList ){
    1511               0 :     return 0;
    1512                 :   }
    1513               0 :   if( pA->pSelect || pB->pSelect ) return 0;
    1514               0 :   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
    1515               0 :   if( pA->token.z ){
    1516               0 :     if( pB->token.z==0 ) return 0;
    1517               0 :     if( pB->token.n!=pA->token.n ) return 0;
    1518               0 :     if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
    1519                 :   }
    1520               0 :   return 1;
    1521                 : }
    1522                 : 
    1523                 : /*
    1524                 : ** Add a new element to the pParse->aAgg[] array and return its index.
    1525                 : */
    1526              14 : static int appendAggInfo(Parse *pParse){
    1527              14 :   if( (pParse->nAgg & 0x7)==0 ){
    1528              14 :     int amt = pParse->nAgg + 8;
    1529              14 :     AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
    1530              14 :     if( aAgg==0 ){
    1531               0 :       return -1;
    1532                 :     }
    1533              14 :     pParse->aAgg = aAgg;
    1534                 :   }
    1535              14 :   memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
    1536              14 :   return pParse->nAgg++;
    1537                 : }
    1538                 : 
    1539                 : /*
    1540                 : ** Analyze the given expression looking for aggregate functions and
    1541                 : ** for variables that need to be added to the pParse->aAgg[] array.
    1542                 : ** Make additional entries to the pParse->aAgg[] array as necessary.
    1543                 : **
    1544                 : ** This routine should only be called after the expression has been
    1545                 : ** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
    1546                 : **
    1547                 : ** If errors are seen, leave an error message in zErrMsg and return
    1548                 : ** the number of errors.
    1549                 : */
    1550              14 : int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
    1551                 :   int i;
    1552                 :   AggExpr *aAgg;
    1553              14 :   int nErr = 0;
    1554                 : 
    1555              14 :   if( pExpr==0 ) return 0;
    1556              14 :   switch( pExpr->op ){
    1557                 :     case TK_COLUMN: {
    1558               0 :       aAgg = pParse->aAgg;
    1559               0 :       for(i=0; i<pParse->nAgg; i++){
    1560               0 :         if( aAgg[i].isAgg ) continue;
    1561               0 :         if( aAgg[i].pExpr->iTable==pExpr->iTable
    1562                 :          && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
    1563               0 :           break;
    1564                 :         }
    1565                 :       }
    1566               0 :       if( i>=pParse->nAgg ){
    1567               0 :         i = appendAggInfo(pParse);
    1568               0 :         if( i<0 ) return 1;
    1569               0 :         pParse->aAgg[i].isAgg = 0;
    1570               0 :         pParse->aAgg[i].pExpr = pExpr;
    1571                 :       }
    1572               0 :       pExpr->iAgg = i;
    1573               0 :       break;
    1574                 :     }
    1575                 :     case TK_AGG_FUNCTION: {
    1576              14 :       aAgg = pParse->aAgg;
    1577              14 :       for(i=0; i<pParse->nAgg; i++){
    1578               0 :         if( !aAgg[i].isAgg ) continue;
    1579               0 :         if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
    1580               0 :           break;
    1581                 :         }
    1582                 :       }
    1583              14 :       if( i>=pParse->nAgg ){
    1584              14 :         i = appendAggInfo(pParse);
    1585              14 :         if( i<0 ) return 1;
    1586              14 :         pParse->aAgg[i].isAgg = 1;
    1587              14 :         pParse->aAgg[i].pExpr = pExpr;
    1588              14 :         pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
    1589                 :              pExpr->token.z, pExpr->token.n,
    1590                 :              pExpr->pList ? pExpr->pList->nExpr : 0, 0);
    1591                 :       }
    1592              14 :       pExpr->iAgg = i;
    1593              14 :       break;
    1594                 :     }
    1595                 :     default: {
    1596               0 :       if( pExpr->pLeft ){
    1597               0 :         nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
    1598                 :       }
    1599               0 :       if( nErr==0 && pExpr->pRight ){
    1600               0 :         nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
    1601                 :       }
    1602               0 :       if( nErr==0 && pExpr->pList ){
    1603               0 :         int n = pExpr->pList->nExpr;
    1604                 :         int i;
    1605               0 :         for(i=0; nErr==0 && i<n; i++){
    1606               0 :           nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
    1607                 :         }
    1608                 :       }
    1609                 :       break;
    1610                 :     }
    1611                 :   }
    1612              14 :   return nErr;
    1613                 : }
    1614                 : 
    1615                 : /*
    1616                 : ** Locate a user function given a name and a number of arguments.
    1617                 : ** Return a pointer to the FuncDef structure that defines that
    1618                 : ** function, or return NULL if the function does not exist.
    1619                 : **
    1620                 : ** If the createFlag argument is true, then a new (blank) FuncDef
    1621                 : ** structure is created and liked into the "db" structure if a
    1622                 : ** no matching function previously existed.  When createFlag is true
    1623                 : ** and the nArg parameter is -1, then only a function that accepts
    1624                 : ** any number of arguments will be returned.
    1625                 : **
    1626                 : ** If createFlag is false and nArg is -1, then the first valid
    1627                 : ** function found is returned.  A function is valid if either xFunc
    1628                 : ** or xStep is non-zero.
    1629                 : */
    1630                 : FuncDef *sqliteFindFunction(
    1631                 :   sqlite *db,        /* An open database */
    1632                 :   const char *zName, /* Name of the function.  Not null-terminated */
    1633                 :   int nName,         /* Number of characters in the name */
    1634                 :   int nArg,          /* Number of arguments.  -1 means any number */
    1635                 :   int createFlag     /* Create new entry if true and does not otherwise exist */
    1636            5500 : ){
    1637                 :   FuncDef *pFirst, *p, *pMaybe;
    1638            5500 :   pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
    1639            5500 :   if( p && !createFlag && nArg<0 ){
    1640               0 :     while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
    1641               0 :     return p;
    1642                 :   }
    1643            5500 :   pMaybe = 0;
    1644           12686 :   while( p && p->nArg!=nArg ){
    1645            1686 :     if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
    1646            1686 :     p = p->pNext;
    1647                 :   }
    1648            5500 :   if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
    1649               0 :     return 0;
    1650                 :   }
    1651            5500 :   if( p==0 && pMaybe ){
    1652                 :     assert( createFlag==0 );
    1653              18 :     return pMaybe;
    1654                 :   }
    1655            5482 :   if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
    1656            5454 :     p->nArg = nArg;
    1657            5454 :     p->pNext = pFirst;
    1658            5454 :     p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
    1659            5454 :     sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
    1660                 :   }
    1661            5482 :   return p;
    1662                 : }

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:21 +0000 (5 days ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.