Main Page | Directories | File List

expr.c

00001 /*
00002 ** 2001 September 15
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This file contains routines used for analyzing expressions and
00013 ** for generating VDBE code that evaluates expressions in SQLite.
00014 **
00015 ** $Id: expr.c,v 1.114.2.4 2004/11/20 20:42:10 drh Exp $
00016 */
00017 #include "sqliteInt.h"
00018 #include <ctype.h>
00019 
00020 /*
00021 ** Construct a new expression node and return a pointer to it.  Memory
00022 ** for this node is obtained from sqliteMalloc().  The calling function
00023 ** is responsible for making sure the node eventually gets freed.
00024 */
00025 Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
00026   Expr *pNew;
00027   pNew = sqliteMalloc( sizeof(Expr) );
00028   if( pNew==0 ){
00029     /* When malloc fails, we leak memory from pLeft and pRight */
00030     return 0;
00031   }
00032   pNew->op = op;
00033   pNew->pLeft = pLeft;
00034   pNew->pRight = pRight;
00035   if( pToken ){
00036     assert( pToken->dyn==0 );
00037     pNew->token = *pToken;
00038     pNew->span = *pToken;
00039   }else{
00040     assert( pNew->token.dyn==0 );
00041     assert( pNew->token.z==0 );
00042     assert( pNew->token.n==0 );
00043     if( pLeft && pRight ){
00044       sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
00045     }else{
00046       pNew->span = pNew->token;
00047     }
00048   }
00049   return pNew;
00050 }
00051 
00052 /*
00053 ** Set the Expr.span field of the given expression to span all
00054 ** text between the two given tokens.
00055 */
00056 void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
00057   assert( pRight!=0 );
00058   assert( pLeft!=0 );
00059   /* Note: pExpr might be NULL due to a prior malloc failure */
00060   if( pExpr && pRight->z && pLeft->z ){
00061     if( pLeft->dyn==0 && pRight->dyn==0 ){
00062       pExpr->span.z = pLeft->z;
00063       pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
00064     }else{
00065       pExpr->span.z = 0;
00066     }
00067   }
00068 }
00069 
00070 /*
00071 ** Construct a new expression node for a function with multiple
00072 ** arguments.
00073 */
00074 Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
00075   Expr *pNew;
00076   pNew = sqliteMalloc( sizeof(Expr) );
00077   if( pNew==0 ){
00078     /* sqliteExprListDelete(pList); // Leak pList when malloc fails */
00079     return 0;
00080   }
00081   pNew->op = TK_FUNCTION;
00082   pNew->pList = pList;
00083   if( pToken ){
00084     assert( pToken->dyn==0 );
00085     pNew->token = *pToken;
00086   }else{
00087     pNew->token.z = 0;
00088   }
00089   pNew->span = pNew->token;
00090   return pNew;
00091 }
00092 
00093 /*
00094 ** Recursively delete an expression tree.
00095 */
00096 void sqliteExprDelete(Expr *p){
00097   if( p==0 ) return;
00098   if( p->span.dyn ) sqliteFree((char*)p->span.z);
00099   if( p->token.dyn ) sqliteFree((char*)p->token.z);
00100   sqliteExprDelete(p->pLeft);
00101   sqliteExprDelete(p->pRight);
00102   sqliteExprListDelete(p->pList);
00103   sqliteSelectDelete(p->pSelect);
00104   sqliteFree(p);
00105 }
00106 
00107 
00108 /*
00109 ** The following group of routines make deep copies of expressions,
00110 ** expression lists, ID lists, and select statements.  The copies can
00111 ** be deleted (by being passed to their respective ...Delete() routines)
00112 ** without effecting the originals.
00113 **
00114 ** The expression list, ID, and source lists return by sqliteExprListDup(),
00115 ** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded 
00116 ** by subsequent calls to sqlite*ListAppend() routines.
00117 **
00118 ** Any tables that the SrcList might point to are not duplicated.
00119 */
00120 Expr *sqliteExprDup(Expr *p){
00121   Expr *pNew;
00122   if( p==0 ) return 0;
00123   pNew = sqliteMallocRaw( sizeof(*p) );
00124   if( pNew==0 ) return 0;
00125   memcpy(pNew, p, sizeof(*pNew));
00126   if( p->token.z!=0 ){
00127     pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
00128     pNew->token.dyn = 1;
00129   }else{
00130     assert( pNew->token.z==0 );
00131   }
00132   pNew->span.z = 0;
00133   pNew->pLeft = sqliteExprDup(p->pLeft);
00134   pNew->pRight = sqliteExprDup(p->pRight);
00135   pNew->pList = sqliteExprListDup(p->pList);
00136   pNew->pSelect = sqliteSelectDup(p->pSelect);
00137   return pNew;
00138 }
00139 void sqliteTokenCopy(Token *pTo, Token *pFrom){
00140   if( pTo->dyn ) sqliteFree((char*)pTo->z);
00141   if( pFrom->z ){
00142     pTo->n = pFrom->n;
00143     pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
00144     pTo->dyn = 1;
00145   }else{
00146     pTo->z = 0;
00147   }
00148 }
00149 ExprList *sqliteExprListDup(ExprList *p){
00150   ExprList *pNew;
00151   struct ExprList_item *pItem;
00152   int i;
00153   if( p==0 ) return 0;
00154   pNew = sqliteMalloc( sizeof(*pNew) );
00155   if( pNew==0 ) return 0;
00156   pNew->nExpr = pNew->nAlloc = p->nExpr;
00157   pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
00158   if( pItem==0 ){
00159     sqliteFree(pNew);
00160     return 0;
00161   }
00162   for(i=0; i<p->nExpr; i++, pItem++){
00163     Expr *pNewExpr, *pOldExpr;
00164     pItem->pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr);
00165     if( pOldExpr->span.z!=0 && pNewExpr ){
00166       /* Always make a copy of the span for top-level expressions in the
00167       ** expression list.  The logic in SELECT processing that determines
00168       ** the names of columns in the result set needs this information */
00169       sqliteTokenCopy(&pNewExpr->span, &pOldExpr->span);
00170     }
00171     assert( pNewExpr==0 || pNewExpr->span.z!=0 
00172             || pOldExpr->span.z==0 || sqlite_malloc_failed );
00173     pItem->zName = sqliteStrDup(p->a[i].zName);
00174     pItem->sortOrder = p->a[i].sortOrder;
00175     pItem->isAgg = p->a[i].isAgg;
00176     pItem->done = 0;
00177   }
00178   return pNew;
00179 }
00180 SrcList *sqliteSrcListDup(SrcList *p){
00181   SrcList *pNew;
00182   int i;
00183   int nByte;
00184   if( p==0 ) return 0;
00185   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
00186   pNew = sqliteMallocRaw( nByte );
00187   if( pNew==0 ) return 0;
00188   pNew->nSrc = pNew->nAlloc = p->nSrc;
00189   for(i=0; i<p->nSrc; i++){
00190     struct SrcList_item *pNewItem = &pNew->a[i];
00191     struct SrcList_item *pOldItem = &p->a[i];
00192     pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
00193     pNewItem->zName = sqliteStrDup(pOldItem->zName);
00194     pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
00195     pNewItem->jointype = pOldItem->jointype;
00196     pNewItem->iCursor = pOldItem->iCursor;
00197     pNewItem->pTab = 0;
00198     pNewItem->pSelect = sqliteSelectDup(pOldItem->pSelect);
00199     pNewItem->pOn = sqliteExprDup(pOldItem->pOn);
00200     pNewItem->pUsing = sqliteIdListDup(pOldItem->pUsing);
00201   }
00202   return pNew;
00203 }
00204 IdList *sqliteIdListDup(IdList *p){
00205   IdList *pNew;
00206   int i;
00207   if( p==0 ) return 0;
00208   pNew = sqliteMallocRaw( sizeof(*pNew) );
00209   if( pNew==0 ) return 0;
00210   pNew->nId = pNew->nAlloc = p->nId;
00211   pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
00212   if( pNew->a==0 ) return 0;
00213   for(i=0; i<p->nId; i++){
00214     struct IdList_item *pNewItem = &pNew->a[i];
00215     struct IdList_item *pOldItem = &p->a[i];
00216     pNewItem->zName = sqliteStrDup(pOldItem->zName);
00217     pNewItem->idx = pOldItem->idx;
00218   }
00219   return pNew;
00220 }
00221 Select *sqliteSelectDup(Select *p){
00222   Select *pNew;
00223   if( p==0 ) return 0;
00224   pNew = sqliteMallocRaw( sizeof(*p) );
00225   if( pNew==0 ) return 0;
00226   pNew->isDistinct = p->isDistinct;
00227   pNew->pEList = sqliteExprListDup(p->pEList);
00228   pNew->pSrc = sqliteSrcListDup(p->pSrc);
00229   pNew->pWhere = sqliteExprDup(p->pWhere);
00230   pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
00231   pNew->pHaving = sqliteExprDup(p->pHaving);
00232   pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
00233   pNew->op = p->op;
00234   pNew->pPrior = sqliteSelectDup(p->pPrior);
00235   pNew->nLimit = p->nLimit;
00236   pNew->nOffset = p->nOffset;
00237   pNew->zSelect = 0;
00238   pNew->iLimit = -1;
00239   pNew->iOffset = -1;
00240   return pNew;
00241 }
00242 
00243 
00244 /*
00245 ** Add a new element to the end of an expression list.  If pList is
00246 ** initially NULL, then create a new expression list.
00247 */
00248 ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
00249   if( pList==0 ){
00250     pList = sqliteMalloc( sizeof(ExprList) );
00251     if( pList==0 ){
00252       /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
00253       return 0;
00254     }
00255     assert( pList->nAlloc==0 );
00256   }
00257   if( pList->nAlloc<=pList->nExpr ){
00258     pList->nAlloc = pList->nAlloc*2 + 4;
00259     pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
00260     if( pList->a==0 ){
00261       /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
00262       pList->nExpr = pList->nAlloc = 0;
00263       return pList;
00264     }
00265   }
00266   assert( pList->a!=0 );
00267   if( pExpr || pName ){
00268     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
00269     memset(pItem, 0, sizeof(*pItem));
00270     pItem->pExpr = pExpr;
00271     if( pName ){
00272       sqliteSetNString(&pItem->zName, pName->z, pName->n, 0);
00273       sqliteDequote(pItem->zName);
00274     }
00275   }
00276   return pList;
00277 }
00278 
00279 /*
00280 ** Delete an entire expression list.
00281 */
00282 void sqliteExprListDelete(ExprList *pList){
00283   int i;
00284   if( pList==0 ) return;
00285   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
00286   assert( pList->nExpr<=pList->nAlloc );
00287   for(i=0; i<pList->nExpr; i++){
00288     sqliteExprDelete(pList->a[i].pExpr);
00289     sqliteFree(pList->a[i].zName);
00290   }
00291   sqliteFree(pList->a);
00292   sqliteFree(pList);
00293 }
00294 
00295 /*
00296 ** Walk an expression tree.  Return 1 if the expression is constant
00297 ** and 0 if it involves variables.
00298 **
00299 ** For the purposes of this function, a double-quoted string (ex: "abc")
00300 ** is considered a variable but a single-quoted string (ex: 'abc') is
00301 ** a constant.
00302 */
00303 int sqliteExprIsConstant(Expr *p){
00304   switch( p->op ){
00305     case TK_ID:
00306     case TK_COLUMN:
00307     case TK_DOT:
00308     case TK_FUNCTION:
00309       return 0;
00310     case TK_NULL:
00311     case TK_STRING:
00312     case TK_INTEGER:
00313     case TK_FLOAT:
00314     case TK_VARIABLE:
00315       return 1;
00316     default: {
00317       if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
00318       if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
00319       if( p->pList ){
00320         int i;
00321         for(i=0; i<p->pList->nExpr; i++){
00322           if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
00323         }
00324       }
00325       return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
00326     }
00327   }
00328   return 0;
00329 }
00330 
00331 /*
00332 ** If the given expression codes a constant integer that is small enough
00333 ** to fit in a 32-bit integer, return 1 and put the value of the integer
00334 ** in *pValue.  If the expression is not an integer or if it is too big
00335 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
00336 */
00337 int sqliteExprIsInteger(Expr *p, int *pValue){
00338   switch( p->op ){
00339     case TK_INTEGER: {
00340       if( sqliteFitsIn32Bits(p->token.z) ){
00341         *pValue = atoi(p->token.z);
00342         return 1;
00343       }
00344       break;
00345     }
00346     case TK_STRING: {
00347       const char *z = p->token.z;
00348       int n = p->token.n;
00349       if( n>0 && z[0]=='-' ){ z++; n--; }
00350       while( n>0 && *z && isdigit(*z) ){ z++; n--; }
00351       if( n==0 && sqliteFitsIn32Bits(p->token.z) ){
00352         *pValue = atoi(p->token.z);
00353         return 1;
00354       }
00355       break;
00356     }
00357     case TK_UPLUS: {
00358       return sqliteExprIsInteger(p->pLeft, pValue);
00359     }
00360     case TK_UMINUS: {
00361       int v;
00362       if( sqliteExprIsInteger(p->pLeft, &v) ){
00363         *pValue = -v;
00364         return 1;
00365       }
00366       break;
00367     }
00368     default: break;
00369   }
00370   return 0;
00371 }
00372 
00373 /*
00374 ** Return TRUE if the given string is a row-id column name.
00375 */
00376 int sqliteIsRowid(const char *z){
00377   if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
00378   if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
00379   if( sqliteStrICmp(z, "OID")==0 ) return 1;
00380   return 0;
00381 }
00382 
00383 /*
00384 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
00385 ** that name in the set of source tables in pSrcList and make the pExpr 
00386 ** expression node refer back to that source column.  The following changes
00387 ** are made to pExpr:
00388 **
00389 **    pExpr->iDb           Set the index in db->aDb[] of the database holding
00390 **                         the table.
00391 **    pExpr->iTable        Set to the cursor number for the table obtained
00392 **                         from pSrcList.
00393 **    pExpr->iColumn       Set to the column number within the table.
00394 **    pExpr->dataType      Set to the appropriate data type for the column.
00395 **    pExpr->op            Set to TK_COLUMN.
00396 **    pExpr->pLeft         Any expression this points to is deleted
00397 **    pExpr->pRight        Any expression this points to is deleted.
00398 **
00399 ** The pDbToken is the name of the database (the "X").  This value may be
00400 ** NULL meaning that name is of the form Y.Z or Z.  Any available database
00401 ** can be used.  The pTableToken is the name of the table (the "Y").  This
00402 ** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
00403 ** means that the form of the name is Z and that columns from any table
00404 ** can be used.
00405 **
00406 ** If the name cannot be resolved unambiguously, leave an error message
00407 ** in pParse and return non-zero.  Return zero on success.
00408 */
00409 static int lookupName(
00410   Parse *pParse,      /* The parsing context */
00411   Token *pDbToken,     /* Name of the database containing table, or NULL */
00412   Token *pTableToken,  /* Name of table containing column, or NULL */
00413   Token *pColumnToken, /* Name of the column. */
00414   SrcList *pSrcList,   /* List of tables used to resolve column names */
00415   ExprList *pEList,    /* List of expressions used to resolve "AS" */
00416   Expr *pExpr          /* Make this EXPR node point to the selected column */
00417 ){
00418   char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
00419   char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
00420   char *zCol = 0;      /* Name of the column.  The "Z" */
00421   int i, j;            /* Loop counters */
00422   int cnt = 0;         /* Number of matching column names */
00423   int cntTab = 0;      /* Number of matching table names */
00424   sqlite *db = pParse->db;  /* The database */
00425 
00426   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
00427   if( pDbToken && pDbToken->z ){
00428     zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
00429     sqliteDequote(zDb);
00430   }else{
00431     zDb = 0;
00432   }
00433   if( pTableToken && pTableToken->z ){
00434     zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
00435     sqliteDequote(zTab);
00436   }else{
00437     assert( zDb==0 );
00438     zTab = 0;
00439   }
00440   zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
00441   sqliteDequote(zCol);
00442   if( sqlite_malloc_failed ){
00443     return 1;  /* Leak memory (zDb and zTab) if malloc fails */
00444   }
00445   assert( zTab==0 || pEList==0 );
00446 
00447   pExpr->iTable = -1;
00448   for(i=0; i<pSrcList->nSrc; i++){
00449     struct SrcList_item *pItem = &pSrcList->a[i];
00450     Table *pTab = pItem->pTab;
00451     Column *pCol;
00452 
00453     if( pTab==0 ) continue;
00454     assert( pTab->nCol>0 );
00455     if( zTab ){
00456       if( pItem->zAlias ){
00457         char *zTabName = pItem->zAlias;
00458         if( sqliteStrICmp(zTabName, zTab)!=0 ) continue;
00459       }else{
00460         char *zTabName = pTab->zName;
00461         if( zTabName==0 || sqliteStrICmp(zTabName, zTab)!=0 ) continue;
00462         if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
00463           continue;
00464         }
00465       }
00466     }
00467     if( 0==(cntTab++) ){
00468       pExpr->iTable = pItem->iCursor;
00469       pExpr->iDb = pTab->iDb;
00470     }
00471     for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
00472       if( sqliteStrICmp(pCol->zName, zCol)==0 ){
00473         cnt++;
00474         pExpr->iTable = pItem->iCursor;
00475         pExpr->iDb = pTab->iDb;
00476         /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
00477         pExpr->iColumn = j==pTab->iPKey ? -1 : j;
00478         pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK;
00479         break;
00480       }
00481     }
00482   }
00483 
00484   /* If we have not already resolved the name, then maybe 
00485   ** it is a new.* or old.* trigger argument reference
00486   */
00487   if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
00488     TriggerStack *pTriggerStack = pParse->trigStack;
00489     Table *pTab = 0;
00490     if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zTab) == 0 ){
00491       pExpr->iTable = pTriggerStack->newIdx;
00492       assert( pTriggerStack->pTab );
00493       pTab = pTriggerStack->pTab;
00494     }else if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zTab) == 0 ){
00495       pExpr->iTable = pTriggerStack->oldIdx;
00496       assert( pTriggerStack->pTab );
00497       pTab = pTriggerStack->pTab;
00498     }
00499 
00500     if( pTab ){ 
00501       int j;
00502       Column *pCol = pTab->aCol;
00503       
00504       pExpr->iDb = pTab->iDb;
00505       cntTab++;
00506       for(j=0; j < pTab->nCol; j++, pCol++) {
00507         if( sqliteStrICmp(pCol->zName, zCol)==0 ){
00508           cnt++;
00509           pExpr->iColumn = j==pTab->iPKey ? -1 : j;
00510           pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK;
00511           break;
00512         }
00513       }
00514     }
00515   }
00516 
00517   /*
00518   ** Perhaps the name is a reference to the ROWID
00519   */
00520   if( cnt==0 && cntTab==1 && sqliteIsRowid(zCol) ){
00521     cnt = 1;
00522     pExpr->iColumn = -1;
00523     pExpr->dataType = SQLITE_SO_NUM;
00524   }
00525 
00526   /*
00527   ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
00528   ** might refer to an result-set alias.  This happens, for example, when
00529   ** we are resolving names in the WHERE clause of the following command:
00530   **
00531   **     SELECT a+b AS x FROM table WHERE x<10;
00532   **
00533   ** In cases like this, replace pExpr with a copy of the expression that
00534   ** forms the result set entry ("a+b" in the example) and return immediately.
00535   ** Note that the expression in the result set should have already been
00536   ** resolved by the time the WHERE clause is resolved.
00537   */
00538   if( cnt==0 && pEList!=0 ){
00539     for(j=0; j<pEList->nExpr; j++){
00540       char *zAs = pEList->a[j].zName;
00541       if( zAs!=0 && sqliteStrICmp(zAs, zCol)==0 ){
00542         assert( pExpr->pLeft==0 && pExpr->pRight==0 );
00543         pExpr->op = TK_AS;
00544         pExpr->iColumn = j;
00545         pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
00546         sqliteFree(zCol);
00547         assert( zTab==0 && zDb==0 );
00548         return 0;
00549       }
00550     } 
00551   }
00552 
00553   /*
00554   ** If X and Y are NULL (in other words if only the column name Z is
00555   ** supplied) and the value of Z is enclosed in double-quotes, then
00556   ** Z is a string literal if it doesn't match any column names.  In that
00557   ** case, we need to return right away and not make any changes to
00558   ** pExpr.
00559   */
00560   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
00561     sqliteFree(zCol);
00562     return 0;
00563   }
00564 
00565   /*
00566   ** cnt==0 means there was not match.  cnt>1 means there were two or
00567   ** more matches.  Either way, we have an error.
00568   */
00569   if( cnt!=1 ){
00570     char *z = 0;
00571     char *zErr;
00572     zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
00573     if( zDb ){
00574       sqliteSetString(&z, zDb, ".", zTab, ".", zCol, 0);
00575     }else if( zTab ){
00576       sqliteSetString(&z, zTab, ".", zCol, 0);
00577     }else{
00578       z = sqliteStrDup(zCol);
00579     }
00580     sqliteErrorMsg(pParse, zErr, z);
00581     sqliteFree(z);
00582   }
00583 
00584   /* Clean up and return
00585   */
00586   sqliteFree(zDb);
00587   sqliteFree(zTab);
00588   sqliteFree(zCol);
00589   sqliteExprDelete(pExpr->pLeft);
00590   pExpr->pLeft = 0;
00591   sqliteExprDelete(pExpr->pRight);
00592   pExpr->pRight = 0;
00593   pExpr->op = TK_COLUMN;
00594   sqliteAuthRead(pParse, pExpr, pSrcList);
00595   return cnt!=1;
00596 }
00597 
00598 /*
00599 ** This routine walks an expression tree and resolves references to
00600 ** table columns.  Nodes of the form ID.ID or ID resolve into an
00601 ** index to the table in the table list and a column offset.  The 
00602 ** Expr.opcode for such nodes is changed to TK_COLUMN.  The Expr.iTable
00603 ** value is changed to the index of the referenced table in pTabList
00604 ** plus the "base" value.  The base value will ultimately become the
00605 ** VDBE cursor number for a cursor that is pointing into the referenced
00606 ** table.  The Expr.iColumn value is changed to the index of the column 
00607 ** of the referenced table.  The Expr.iColumn value for the special
00608 ** ROWID column is -1.  Any INTEGER PRIMARY KEY column is tried as an
00609 ** alias for ROWID.
00610 **
00611 ** We also check for instances of the IN operator.  IN comes in two
00612 ** forms:
00613 **
00614 **           expr IN (exprlist)
00615 ** and
00616 **           expr IN (SELECT ...)
00617 **
00618 ** The first form is handled by creating a set holding the list
00619 ** of allowed values.  The second form causes the SELECT to generate 
00620 ** a temporary table.
00621 **
00622 ** This routine also looks for scalar SELECTs that are part of an expression.
00623 ** If it finds any, it generates code to write the value of that select
00624 ** into a memory cell.
00625 **
00626 ** Unknown columns or tables provoke an error.  The function returns
00627 ** the number of errors seen and leaves an error message on pParse->zErrMsg.
00628 */
00629 int sqliteExprResolveIds(
00630   Parse *pParse,     /* The parser context */
00631   SrcList *pSrcList, /* List of tables used to resolve column names */
00632   ExprList *pEList,  /* List of expressions used to resolve "AS" */
00633   Expr *pExpr        /* The expression to be analyzed. */
00634 ){
00635   int i;
00636 
00637   if( pExpr==0 || pSrcList==0 ) return 0;
00638   for(i=0; i<pSrcList->nSrc; i++){
00639     assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
00640   }
00641   switch( pExpr->op ){
00642     /* Double-quoted strings (ex: "abc") are used as identifiers if
00643     ** possible.  Otherwise they remain as strings.  Single-quoted
00644     ** strings (ex: 'abc') are always string literals.
00645     */
00646     case TK_STRING: {
00647       if( pExpr->token.z[0]=='\'' ) break;
00648       /* Fall thru into the TK_ID case if this is a double-quoted string */
00649     }
00650     /* A lone identifier is the name of a columnd.
00651     */
00652     case TK_ID: {
00653       if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
00654         return 1;
00655       }
00656       break; 
00657     }
00658   
00659     /* A table name and column name:     ID.ID
00660     ** Or a database, table and column:  ID.ID.ID
00661     */
00662     case TK_DOT: {
00663       Token *pColumn;
00664       Token *pTable;
00665       Token *pDb;
00666       Expr *pRight;
00667 
00668       pRight = pExpr->pRight;
00669       if( pRight->op==TK_ID ){
00670         pDb = 0;
00671         pTable = &pExpr->pLeft->token;
00672         pColumn = &pRight->token;
00673       }else{
00674         assert( pRight->op==TK_DOT );
00675         pDb = &pExpr->pLeft->token;
00676         pTable = &pRight->pLeft->token;
00677         pColumn = &pRight->pRight->token;
00678       }
00679       if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
00680         return 1;
00681       }
00682       break;
00683     }
00684 
00685     case TK_IN: {
00686       Vdbe *v = sqliteGetVdbe(pParse);
00687       if( v==0 ) return 1;
00688       if( sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
00689         return 1;
00690       }
00691       if( pExpr->pSelect ){
00692         /* Case 1:     expr IN (SELECT ...)
00693         **
00694         ** Generate code to write the results of the select into a temporary
00695         ** table.  The cursor number of the temporary table has already
00696         ** been put in iTable by sqliteExprResolveInSelect().
00697         */
00698         pExpr->iTable = pParse->nTab++;
00699         sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
00700         sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
00701       }else if( pExpr->pList ){
00702         /* Case 2:     expr IN (exprlist)
00703         **
00704         ** Create a set to put the exprlist values in.  The Set id is stored
00705         ** in iTable.
00706         */
00707         int i, iSet;
00708         for(i=0; i<pExpr->pList->nExpr; i++){
00709           Expr *pE2 = pExpr->pList->a[i].pExpr;
00710           if( !sqliteExprIsConstant(pE2) ){
00711             sqliteErrorMsg(pParse,
00712               "right-hand side of IN operator must be constant");
00713             return 1;
00714           }
00715           if( sqliteExprCheck(pParse, pE2, 0, 0) ){
00716             return 1;
00717           }
00718         }
00719         iSet = pExpr->iTable = pParse->nSet++;
00720         for(i=0; i<pExpr->pList->nExpr; i++){
00721           Expr *pE2 = pExpr->pList->a[i].pExpr;
00722           switch( pE2->op ){
00723             case TK_FLOAT:
00724             case TK_INTEGER:
00725             case TK_STRING: {
00726               int addr;
00727               assert( pE2->token.z );
00728               addr = sqliteVdbeOp3(v, OP_SetInsert, iSet, 0,
00729                                   pE2->token.z, pE2->token.n);
00730               sqliteVdbeDequoteP3(v, addr);
00731               break;
00732             }
00733             default: {
00734               sqliteExprCode(pParse, pE2);
00735               sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
00736               break;
00737             }
00738           }
00739         }
00740       }
00741       break;
00742     }
00743 
00744     case TK_SELECT: {
00745       /* This has to be a scalar SELECT.  Generate code to put the
00746       ** value of this select in a memory cell and record the number
00747       ** of the memory cell in iColumn.
00748       */
00749       pExpr->iColumn = pParse->nMem++;
00750       if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
00751         return 1;
00752       }
00753       break;
00754     }
00755 
00756     /* For all else, just recursively walk the tree */
00757     default: {
00758       if( pExpr->pLeft
00759       && sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
00760         return 1;
00761       }
00762       if( pExpr->pRight 
00763       && sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
00764         return 1;
00765       }
00766       if( pExpr->pList ){
00767         int i;
00768         ExprList *pList = pExpr->pList;
00769         for(i=0; i<pList->nExpr; i++){
00770           Expr *pArg = pList->a[i].pExpr;
00771           if( sqliteExprResolveIds(pParse, pSrcList, pEList, pArg) ){
00772             return 1;
00773           }
00774         }
00775       }
00776     }
00777   }
00778   return 0;
00779 }
00780 
00781 /*
00782 ** pExpr is a node that defines a function of some kind.  It might
00783 ** be a syntactic function like "count(x)" or it might be a function
00784 ** that implements an operator, like "a LIKE b".  
00785 **
00786 ** This routine makes *pzName point to the name of the function and 
00787 ** *pnName hold the number of characters in the function name.
00788 */
00789 static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
00790   switch( pExpr->op ){
00791     case TK_FUNCTION: {
00792       *pzName = pExpr->token.z;
00793       *pnName = pExpr->token.n;
00794       break;
00795     }
00796     case TK_LIKE: {
00797       *pzName = "like";
00798       *pnName = 4;
00799       break;
00800     }
00801     case TK_GLOB: {
00802       *pzName = "glob";
00803       *pnName = 4;
00804       break;
00805     }
00806     default: {
00807       *pzName = "can't happen";
00808       *pnName = 12;
00809       break;
00810     }
00811   }
00812 }
00813 
00814 /*
00815 ** Error check the functions in an expression.  Make sure all
00816 ** function names are recognized and all functions have the correct
00817 ** number of arguments.  Leave an error message in pParse->zErrMsg
00818 ** if anything is amiss.  Return the number of errors.
00819 **
00820 ** if pIsAgg is not null and this expression is an aggregate function
00821 ** (like count(*) or max(value)) then write a 1 into *pIsAgg.
00822 */
00823 int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
00824   int nErr = 0;
00825   if( pExpr==0 ) return 0;
00826   switch( pExpr->op ){
00827     case TK_GLOB:
00828     case TK_LIKE:
00829     case TK_FUNCTION: {
00830       int n = pExpr->pList ? pExpr->pList->nExpr : 0;  /* Number of arguments */
00831       int no_such_func = 0;       /* True if no such function exists */
00832       int wrong_num_args = 0;     /* True if wrong number of arguments */
00833       int is_agg = 0;             /* True if is an aggregate function */
00834       int i;
00835       int nId;                    /* Number of characters in function name */
00836       const char *zId;            /* The function name. */
00837       FuncDef *pDef;
00838 
00839       getFunctionName(pExpr, &zId, &nId);
00840       pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
00841       if( pDef==0 ){
00842         pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
00843         if( pDef==0 ){
00844           no_such_func = 1;
00845         }else{
00846           wrong_num_args = 1;
00847         }
00848       }else{
00849         is_agg = pDef->xFunc==0;
00850       }
00851       if( is_agg && !allowAgg ){
00852         sqliteErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
00853         nErr++;
00854         is_agg = 0;
00855       }else if( no_such_func ){
00856         sqliteErrorMsg(pParse, "no such function: %.*s", nId, zId);
00857         nErr++;
00858       }else if( wrong_num_args ){
00859         sqliteErrorMsg(pParse,"wrong number of arguments to function %.*s()",
00860              nId, zId);
00861         nErr++;
00862       }
00863       if( is_agg ){
00864         pExpr->op = TK_AGG_FUNCTION;
00865         if( pIsAgg ) *pIsAgg = 1;
00866       }
00867       for(i=0; nErr==0 && i<n; i++){
00868         nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
00869                                allowAgg && !is_agg, pIsAgg);
00870       }
00871       if( pDef==0 ){
00872         /* Already reported an error */
00873       }else if( pDef->dataType>=0 ){
00874         if( pDef->dataType<n ){
00875           pExpr->dataType = 
00876              sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
00877         }else{
00878           pExpr->dataType = SQLITE_SO_NUM;
00879         }
00880       }else if( pDef->dataType==SQLITE_ARGS ){
00881         pDef->dataType = SQLITE_SO_TEXT;
00882         for(i=0; i<n; i++){
00883           if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
00884             pExpr->dataType = SQLITE_SO_NUM;
00885             break;
00886           }
00887         }
00888       }else if( pDef->dataType==SQLITE_NUMERIC ){
00889         pExpr->dataType = SQLITE_SO_NUM;
00890       }else{
00891         pExpr->dataType = SQLITE_SO_TEXT;
00892       }
00893     }
00894     default: {
00895       if( pExpr->pLeft ){
00896         nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
00897       }
00898       if( nErr==0 && pExpr->pRight ){
00899         nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
00900       }
00901       if( nErr==0 && pExpr->pList ){
00902         int n = pExpr->pList->nExpr;
00903         int i;
00904         for(i=0; nErr==0 && i<n; i++){
00905           Expr *pE2 = pExpr->pList->a[i].pExpr;
00906           nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
00907         }
00908       }
00909       break;
00910     }
00911   }
00912   return nErr;
00913 }
00914 
00915 /*
00916 ** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
00917 ** given expression should sort as numeric values or as text.
00918 **
00919 ** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
00920 ** both been called on the expression before it is passed to this routine.
00921 */
00922 int sqliteExprType(Expr *p){
00923   if( p==0 ) return SQLITE_SO_NUM;
00924   while( p ) switch( p->op ){
00925     case TK_PLUS:
00926     case TK_MINUS:
00927     case TK_STAR:
00928     case TK_SLASH:
00929     case TK_AND:
00930     case TK_OR:
00931     case TK_ISNULL:
00932     case TK_NOTNULL:
00933     case TK_NOT:
00934     case TK_UMINUS:
00935     case TK_UPLUS:
00936     case TK_BITAND:
00937     case TK_BITOR:
00938     case TK_BITNOT:
00939     case TK_LSHIFT:
00940     case TK_RSHIFT:
00941     case TK_REM:
00942     case TK_INTEGER:
00943     case TK_FLOAT:
00944     case TK_IN:
00945     case TK_BETWEEN:
00946     case TK_GLOB:
00947     case TK_LIKE:
00948       return SQLITE_SO_NUM;
00949 
00950     case TK_STRING:
00951     case TK_NULL:
00952     case TK_CONCAT:
00953     case TK_VARIABLE:
00954       return SQLITE_SO_TEXT;
00955 
00956     case TK_LT:
00957     case TK_LE:
00958     case TK_GT:
00959     case TK_GE:
00960     case TK_NE:
00961     case TK_EQ:
00962       if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
00963         return SQLITE_SO_NUM;
00964       }
00965       p = p->pRight;
00966       break;
00967 
00968     case TK_AS:
00969       p = p->pLeft;
00970       break;
00971 
00972     case TK_COLUMN:
00973     case TK_FUNCTION:
00974     case TK_AGG_FUNCTION:
00975       return p->dataType;
00976 
00977     case TK_SELECT:
00978       assert( p->pSelect );
00979       assert( p->pSelect->pEList );
00980       assert( p->pSelect->pEList->nExpr>0 );
00981       p = p->pSelect->pEList->a[0].pExpr;
00982       break;
00983 
00984     case TK_CASE: {
00985       if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
00986         return SQLITE_SO_NUM;
00987       }
00988       if( p->pList ){
00989         int i;
00990         ExprList *pList = p->pList;
00991         for(i=1; i<pList->nExpr; i+=2){
00992           if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
00993             return SQLITE_SO_NUM;
00994           }
00995         }
00996       }
00997       return SQLITE_SO_TEXT;
00998     }
00999 
01000     default:
01001       assert( p->op==TK_ABORT );  /* Can't Happen */
01002       break;
01003   }
01004   return SQLITE_SO_NUM;
01005 }
01006 
01007 /*
01008 ** Generate code into the current Vdbe to evaluate the given
01009 ** expression and leave the result on the top of stack.
01010 */
01011 void sqliteExprCode(Parse *pParse, Expr *pExpr){
01012   Vdbe *v = pParse->pVdbe;
01013   int op;
01014   if( v==0 || pExpr==0 ) return;
01015   switch( pExpr->op ){
01016     case TK_PLUS:     op = OP_Add;      break;
01017     case TK_MINUS:    op = OP_Subtract; break;
01018     case TK_STAR:     op = OP_Multiply; break;
01019     case TK_SLASH:    op = OP_Divide;   break;
01020     case TK_AND:      op = OP_And;      break;
01021     case TK_OR:       op = OP_Or;       break;
01022     case TK_LT:       op = OP_Lt;       break;
01023     case TK_LE:       op = OP_Le;       break;
01024     case TK_GT:       op = OP_Gt;       break;
01025     case TK_GE:       op = OP_Ge;       break;
01026     case TK_NE:       op = OP_Ne;       break;
01027     case TK_EQ:       op = OP_Eq;       break;
01028     case TK_ISNULL:   op = OP_IsNull;   break;
01029     case TK_NOTNULL:  op = OP_NotNull;  break;
01030     case TK_NOT:      op = OP_Not;      break;
01031     case TK_UMINUS:   op = OP_Negative; break;
01032     case TK_BITAND:   op = OP_BitAnd;   break;
01033     case TK_BITOR:    op = OP_BitOr;    break;
01034     case TK_BITNOT:   op = OP_BitNot;   break;
01035     case TK_LSHIFT:   op = OP_ShiftLeft;  break;
01036     case TK_RSHIFT:   op = OP_ShiftRight; break;
01037     case TK_REM:      op = OP_Remainder;  break;
01038     default: break;
01039   }
01040   switch( pExpr->op ){
01041     case TK_COLUMN: {
01042       if( pParse->useAgg ){
01043         sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
01044       }else if( pExpr->iColumn>=0 ){
01045         sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
01046       }else{
01047         sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
01048       }
01049       break;
01050     }
01051     case TK_STRING:
01052     case TK_FLOAT:
01053     case TK_INTEGER: {
01054       if( pExpr->op==TK_INTEGER && sqliteFitsIn32Bits(pExpr->token.z) ){
01055         sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
01056       }else{
01057         sqliteVdbeAddOp(v, OP_String, 0, 0);
01058       }
01059       assert( pExpr->token.z );
01060       sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
01061       sqliteVdbeDequoteP3(v, -1);
01062       break;
01063     }
01064     case TK_NULL: {
01065       sqliteVdbeAddOp(v, OP_String, 0, 0);
01066       break;
01067     }
01068     case TK_VARIABLE: {
01069       sqliteVdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
01070       break;
01071     }
01072     case TK_LT:
01073     case TK_LE:
01074     case TK_GT:
01075     case TK_GE:
01076     case TK_NE:
01077     case TK_EQ: {
01078       if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
01079         op += 6;  /* Convert numeric opcodes to text opcodes */
01080       }
01081       /* Fall through into the next case */
01082     }
01083     case TK_AND:
01084     case TK_OR:
01085     case TK_PLUS:
01086     case TK_STAR:
01087     case TK_MINUS:
01088     case TK_REM:
01089     case TK_BITAND:
01090     case TK_BITOR:
01091     case TK_SLASH: {
01092       sqliteExprCode(pParse, pExpr->pLeft);
01093       sqliteExprCode(pParse, pExpr->pRight);
01094       sqliteVdbeAddOp(v, op, 0, 0);
01095       break;
01096     }
01097     case TK_LSHIFT:
01098     case TK_RSHIFT: {
01099       sqliteExprCode(pParse, pExpr->pRight);
01100       sqliteExprCode(pParse, pExpr->pLeft);
01101       sqliteVdbeAddOp(v, op, 0, 0);
01102       break;
01103     }
01104     case TK_CONCAT: {
01105       sqliteExprCode(pParse, pExpr->pLeft);
01106       sqliteExprCode(pParse, pExpr->pRight);
01107       sqliteVdbeAddOp(v, OP_Concat, 2, 0);
01108       break;
01109     }
01110     case TK_UMINUS: {
01111       assert( pExpr->pLeft );
01112       if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
01113         Token *p = &pExpr->pLeft->token;
01114         char *z = sqliteMalloc( p->n + 2 );
01115         sprintf(z, "-%.*s", p->n, p->z);
01116         if( pExpr->pLeft->op==TK_INTEGER && sqliteFitsIn32Bits(z) ){
01117           sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
01118         }else{
01119           sqliteVdbeAddOp(v, OP_String, 0, 0);
01120         }
01121         sqliteVdbeChangeP3(v, -1, z, p->n+1);
01122         sqliteFree(z);
01123         break;
01124       }
01125       /* Fall through into TK_NOT */
01126     }
01127     case TK_BITNOT:
01128     case TK_NOT: {
01129       sqliteExprCode(pParse, pExpr->pLeft);
01130       sqliteVdbeAddOp(v, op, 0, 0);
01131       break;
01132     }
01133     case TK_ISNULL:
01134     case TK_NOTNULL: {
01135       int dest;
01136       sqliteVdbeAddOp(v, OP_Integer, 1, 0);
01137       sqliteExprCode(pParse, pExpr->pLeft);
01138       dest = sqliteVdbeCurrentAddr(v) + 2;
01139       sqliteVdbeAddOp(v, op, 1, dest);
01140       sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
01141       break;
01142     }
01143     case TK_AGG_FUNCTION: {
01144       sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
01145       break;
01146     }
01147     case TK_GLOB:
01148     case TK_LIKE:
01149     case TK_FUNCTION: {
01150       ExprList *pList = pExpr->pList;
01151       int nExpr = pList ? pList->nExpr : 0;
01152       FuncDef *pDef;
01153       int nId;
01154       const char *zId;
01155       getFunctionName(pExpr, &zId, &nId);
01156       pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
01157       assert( pDef!=0 );
01158       nExpr = sqliteExprCodeExprList(pParse, pList, pDef->includeTypes);
01159       sqliteVdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER);
01160       break;
01161     }
01162     case TK_SELECT: {
01163       sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
01164       break;
01165     }
01166     case TK_IN: {
01167       int addr;
01168       sqliteVdbeAddOp(v, OP_Integer, 1, 0);
01169       sqliteExprCode(pParse, pExpr->pLeft);
01170       addr = sqliteVdbeCurrentAddr(v);
01171       sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
01172       sqliteVdbeAddOp(v, OP_Pop, 2, 0);
01173       sqliteVdbeAddOp(v, OP_String, 0, 0);
01174       sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
01175       if( pExpr->pSelect ){
01176         sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
01177       }else{
01178         sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
01179       }
01180       sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
01181       break;
01182     }
01183     case TK_BETWEEN: {
01184       sqliteExprCode(pParse, pExpr->pLeft);
01185       sqliteVdbeAddOp(v, OP_Dup, 0, 0);
01186       sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
01187       sqliteVdbeAddOp(v, OP_Ge, 0, 0);
01188       sqliteVdbeAddOp(v, OP_Pull, 1, 0);
01189       sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
01190       sqliteVdbeAddOp(v, OP_Le, 0, 0);
01191       sqliteVdbeAddOp(v, OP_And, 0, 0);
01192       break;
01193     }
01194     case TK_UPLUS:
01195     case TK_AS: {
01196       sqliteExprCode(pParse, pExpr->pLeft);
01197       break;
01198     }
01199     case TK_CASE: {
01200       int expr_end_label;
01201       int jumpInst;
01202       int addr;
01203       int nExpr;
01204       int i;
01205 
01206       assert(pExpr->pList);
01207       assert((pExpr->pList->nExpr % 2) == 0);
01208       assert(pExpr->pList->nExpr > 0);
01209       nExpr = pExpr->pList->nExpr;
01210       expr_end_label = sqliteVdbeMakeLabel(v);
01211       if( pExpr->pLeft ){
01212         sqliteExprCode(pParse, pExpr->pLeft);
01213       }
01214       for(i=0; i<nExpr; i=i+2){
01215         sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
01216         if( pExpr->pLeft ){
01217           sqliteVdbeAddOp(v, OP_Dup, 1, 1);
01218           jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
01219           sqliteVdbeAddOp(v, OP_Pop, 1, 0);
01220         }else{
01221           jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
01222         }
01223         sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
01224         sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
01225         addr = sqliteVdbeCurrentAddr(v);
01226         sqliteVdbeChangeP2(v, jumpInst, addr);
01227       }
01228       if( pExpr->pLeft ){
01229         sqliteVdbeAddOp(v, OP_Pop, 1, 0);
01230       }
01231       if( pExpr->pRight ){
01232         sqliteExprCode(pParse, pExpr->pRight);
01233       }else{
01234         sqliteVdbeAddOp(v, OP_String, 0, 0);
01235       }
01236       sqliteVdbeResolveLabel(v, expr_end_label);
01237       break;
01238     }
01239     case TK_RAISE: {
01240       if( !pParse->trigStack ){
01241         sqliteErrorMsg(pParse,
01242                        "RAISE() may only be used within a trigger-program");
01243         pParse->nErr++;
01244         return;
01245       }
01246       if( pExpr->iColumn == OE_Rollback ||
01247           pExpr->iColumn == OE_Abort ||
01248           pExpr->iColumn == OE_Fail ){
01249           sqliteVdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
01250                            pExpr->token.z, pExpr->token.n);
01251           sqliteVdbeDequoteP3(v, -1);
01252       } else {
01253           assert( pExpr->iColumn == OE_Ignore );
01254           sqliteVdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
01255                            "(IGNORE jump)", 0);
01256       }
01257     }
01258     break;
01259   }
01260 }
01261 
01262 /*
01263 ** Generate code that pushes the value of every element of the given
01264 ** expression list onto the stack.  If the includeTypes flag is true,
01265 ** then also push a string that is the datatype of each element onto
01266 ** the stack after the value.
01267 **
01268 ** Return the number of elements pushed onto the stack.
01269 */
01270 int sqliteExprCodeExprList(
01271   Parse *pParse,     /* Parsing context */
01272   ExprList *pList,   /* The expression list to be coded */
01273   int includeTypes   /* TRUE to put datatypes on the stack too */
01274 ){
01275   struct ExprList_item *pItem;
01276   int i, n;
01277   Vdbe *v;
01278   if( pList==0 ) return 0;
01279   v = sqliteGetVdbe(pParse);
01280   n = pList->nExpr;
01281   for(pItem=pList->a, i=0; i<n; i++, pItem++){
01282     sqliteExprCode(pParse, pItem->pExpr);
01283     if( includeTypes ){
01284       sqliteVdbeOp3(v, OP_String, 0, 0, 
01285          sqliteExprType(pItem->pExpr)==SQLITE_SO_NUM ? "numeric" : "text",
01286          P3_STATIC);
01287     }
01288   }
01289   return includeTypes ? n*2 : n;
01290 }
01291 
01292 /*
01293 ** Generate code for a boolean expression such that a jump is made
01294 ** to the label "dest" if the expression is true but execution
01295 ** continues straight thru if the expression is false.
01296 **
01297 ** If the expression evaluates to NULL (neither true nor false), then
01298 ** take the jump if the jumpIfNull flag is true.
01299 */
01300 void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
01301   Vdbe *v = pParse->pVdbe;
01302   int op = 0;
01303   if( v==0 || pExpr==0 ) return;
01304   switch( pExpr->op ){
01305     case TK_LT:       op = OP_Lt;       break;
01306     case TK_LE:       op = OP_Le;       break;
01307     case TK_GT:       op = OP_Gt;       break;
01308     case TK_GE:       op = OP_Ge;       break;
01309     case TK_NE:       op = OP_Ne;       break;
01310     case TK_EQ:       op = OP_Eq;       break;
01311     case TK_ISNULL:   op = OP_IsNull;   break;
01312     case TK_NOTNULL:  op = OP_NotNull;  break;
01313     default:  break;
01314   }
01315   switch( pExpr->op ){
01316     case TK_AND: {
01317       int d2 = sqliteVdbeMakeLabel(v);
01318       sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
01319       sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
01320       sqliteVdbeResolveLabel(v, d2);
01321       break;
01322     }
01323     case TK_OR: {
01324       sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
01325       sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
01326       break;
01327     }
01328     case TK_NOT: {
01329       sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
01330       break;
01331     }
01332     case TK_LT:
01333     case TK_LE:
01334     case TK_GT:
01335     case TK_GE:
01336     case TK_NE:
01337     case TK_EQ: {
01338       sqliteExprCode(pParse, pExpr->pLeft);
01339       sqliteExprCode(pParse, pExpr->pRight);
01340       if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
01341         op += 6;  /* Convert numeric opcodes to text opcodes */
01342       }
01343       sqliteVdbeAddOp(v, op, jumpIfNull, dest);
01344       break;
01345     }
01346     case TK_ISNULL:
01347     case TK_NOTNULL: {
01348       sqliteExprCode(pParse, pExpr->pLeft);
01349       sqliteVdbeAddOp(v, op, 1, dest);
01350       break;
01351     }
01352     case TK_IN: {
01353       int addr;
01354       sqliteExprCode(pParse, pExpr->pLeft);
01355       addr = sqliteVdbeCurrentAddr(v);
01356       sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
01357       sqliteVdbeAddOp(v, OP_Pop, 1, 0);
01358       sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
01359       if( pExpr->pSelect ){
01360         sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
01361       }else{
01362         sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
01363       }
01364       break;
01365     }
01366     case TK_BETWEEN: {
01367       int addr;
01368       sqliteExprCode(pParse, pExpr->pLeft);
01369       sqliteVdbeAddOp(v, OP_Dup, 0, 0);
01370       sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
01371       addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
01372       sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
01373       sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
01374       sqliteVdbeAddOp(v, OP_Integer, 0, 0);
01375       sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
01376       sqliteVdbeAddOp(v, OP_Pop, 1, 0);
01377       break;
01378     }
01379     default: {
01380       sqliteExprCode(pParse, pExpr);
01381       sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
01382       break;
01383     }
01384   }
01385 }
01386 
01387 /*
01388 ** Generate code for a boolean expression such that a jump is made
01389 ** to the label "dest" if the expression is false but execution
01390 ** continues straight thru if the expression is true.
01391 **
01392 ** If the expression evaluates to NULL (neither true nor false) then
01393 ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
01394 */
01395 void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
01396   Vdbe *v = pParse->pVdbe;
01397   int op = 0;
01398   if( v==0 || pExpr==0 ) return;
01399   switch( pExpr->op ){
01400     case TK_LT:       op = OP_Ge;       break;
01401     case TK_LE:       op = OP_Gt;       break;
01402     case TK_GT:       op = OP_Le;       break;
01403     case TK_GE:       op = OP_Lt;       break;
01404     case TK_NE:       op = OP_Eq;       break;
01405     case TK_EQ:       op = OP_Ne;       break;
01406     case TK_ISNULL:   op = OP_NotNull;  break;
01407     case TK_NOTNULL:  op = OP_IsNull;   break;
01408     default:  break;
01409   }
01410   switch( pExpr->op ){
01411     case TK_AND: {
01412       sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
01413       sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
01414       break;
01415     }
01416     case TK_OR: {
01417       int d2 = sqliteVdbeMakeLabel(v);
01418       sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
01419       sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
01420       sqliteVdbeResolveLabel(v, d2);
01421       break;
01422     }
01423     case TK_NOT: {
01424       sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
01425       break;
01426     }
01427     case TK_LT:
01428     case TK_LE:
01429     case TK_GT:
01430     case TK_GE:
01431     case TK_NE:
01432     case TK_EQ: {
01433       if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
01434         /* Convert numeric comparison opcodes into text comparison opcodes.
01435         ** This step depends on the fact that the text comparision opcodes are
01436         ** always 6 greater than their corresponding numeric comparison
01437         ** opcodes.
01438         */
01439         assert( OP_Eq+6 == OP_StrEq );
01440         op += 6;
01441       }
01442       sqliteExprCode(pParse, pExpr->pLeft);
01443       sqliteExprCode(pParse, pExpr->pRight);
01444       sqliteVdbeAddOp(v, op, jumpIfNull, dest);
01445       break;
01446     }
01447     case TK_ISNULL:
01448     case TK_NOTNULL: {
01449       sqliteExprCode(pParse, pExpr->pLeft);
01450       sqliteVdbeAddOp(v, op, 1, dest);
01451       break;
01452     }
01453     case TK_IN: {
01454       int addr;
01455       sqliteExprCode(pParse, pExpr->pLeft);
01456       addr = sqliteVdbeCurrentAddr(v);
01457       sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
01458       sqliteVdbeAddOp(v, OP_Pop, 1, 0);
01459       sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
01460       if( pExpr->pSelect ){
01461         sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
01462       }else{
01463         sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
01464       }
01465       break;
01466     }
01467     case TK_BETWEEN: {
01468       int addr;
01469       sqliteExprCode(pParse, pExpr->pLeft);
01470       sqliteVdbeAddOp(v, OP_Dup, 0, 0);
01471       sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
01472       addr = sqliteVdbeCurrentAddr(v);
01473       sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
01474       sqliteVdbeAddOp(v, OP_Pop, 1, 0);
01475       sqliteVdbeAddOp(v, OP_Goto, 0, dest);
01476       sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
01477       sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
01478       break;
01479     }
01480     default: {
01481       sqliteExprCode(pParse, pExpr);
01482       sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
01483       break;
01484     }
01485   }
01486 }
01487 
01488 /*
01489 ** Do a deep comparison of two expression trees.  Return TRUE (non-zero)
01490 ** if they are identical and return FALSE if they differ in any way.
01491 */
01492 int sqliteExprCompare(Expr *pA, Expr *pB){
01493   int i;
01494   if( pA==0 ){
01495     return pB==0;
01496   }else if( pB==0 ){
01497     return 0;
01498   }
01499   if( pA->op!=pB->op ) return 0;
01500   if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
01501   if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
01502   if( pA->pList ){
01503     if( pB->pList==0 ) return 0;
01504     if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
01505     for(i=0; i<pA->pList->nExpr; i++){
01506       if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
01507         return 0;
01508       }
01509     }
01510   }else if( pB->pList ){
01511     return 0;
01512   }
01513   if( pA->pSelect || pB->pSelect ) return 0;
01514   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
01515   if( pA->token.z ){
01516     if( pB->token.z==0 ) return 0;
01517     if( pB->token.n!=pA->token.n ) return 0;
01518     if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
01519   }
01520   return 1;
01521 }
01522 
01523 /*
01524 ** Add a new element to the pParse->aAgg[] array and return its index.
01525 */
01526 static int appendAggInfo(Parse *pParse){
01527   if( (pParse->nAgg & 0x7)==0 ){
01528     int amt = pParse->nAgg + 8;
01529     AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
01530     if( aAgg==0 ){
01531       return -1;
01532     }
01533     pParse->aAgg = aAgg;
01534   }
01535   memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
01536   return pParse->nAgg++;
01537 }
01538 
01539 /*
01540 ** Analyze the given expression looking for aggregate functions and
01541 ** for variables that need to be added to the pParse->aAgg[] array.
01542 ** Make additional entries to the pParse->aAgg[] array as necessary.
01543 **
01544 ** This routine should only be called after the expression has been
01545 ** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
01546 **
01547 ** If errors are seen, leave an error message in zErrMsg and return
01548 ** the number of errors.
01549 */
01550 int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
01551   int i;
01552   AggExpr *aAgg;
01553   int nErr = 0;
01554 
01555   if( pExpr==0 ) return 0;
01556   switch( pExpr->op ){
01557     case TK_COLUMN: {
01558       aAgg = pParse->aAgg;
01559       for(i=0; i<pParse->nAgg; i++){
01560         if( aAgg[i].isAgg ) continue;
01561         if( aAgg[i].pExpr->iTable==pExpr->iTable
01562          && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
01563           break;
01564         }
01565       }
01566       if( i>=pParse->nAgg ){
01567         i = appendAggInfo(pParse);
01568         if( i<0 ) return 1;
01569         pParse->aAgg[i].isAgg = 0;
01570         pParse->aAgg[i].pExpr = pExpr;
01571       }
01572       pExpr->iAgg = i;
01573       break;
01574     }
01575     case TK_AGG_FUNCTION: {
01576       aAgg = pParse->aAgg;
01577       for(i=0; i<pParse->nAgg; i++){
01578         if( !aAgg[i].isAgg ) continue;
01579         if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
01580           break;
01581         }
01582       }
01583       if( i>=pParse->nAgg ){
01584         i = appendAggInfo(pParse);
01585         if( i<0 ) return 1;
01586         pParse->aAgg[i].isAgg = 1;
01587         pParse->aAgg[i].pExpr = pExpr;
01588         pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
01589              pExpr->token.z, pExpr->token.n,
01590              pExpr->pList ? pExpr->pList->nExpr : 0, 0);
01591       }
01592       pExpr->iAgg = i;
01593       break;
01594     }
01595     default: {
01596       if( pExpr->pLeft ){
01597         nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
01598       }
01599       if( nErr==0 && pExpr->pRight ){
01600         nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
01601       }
01602       if( nErr==0 && pExpr->pList ){
01603         int n = pExpr->pList->nExpr;
01604         int i;
01605         for(i=0; nErr==0 && i<n; i++){
01606           nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
01607         }
01608       }
01609       break;
01610     }
01611   }
01612   return nErr;
01613 }
01614 
01615 /*
01616 ** Locate a user function given a name and a number of arguments.
01617 ** Return a pointer to the FuncDef structure that defines that
01618 ** function, or return NULL if the function does not exist.
01619 **
01620 ** If the createFlag argument is true, then a new (blank) FuncDef
01621 ** structure is created and liked into the "db" structure if a
01622 ** no matching function previously existed.  When createFlag is true
01623 ** and the nArg parameter is -1, then only a function that accepts
01624 ** any number of arguments will be returned.
01625 **
01626 ** If createFlag is false and nArg is -1, then the first valid
01627 ** function found is returned.  A function is valid if either xFunc
01628 ** or xStep is non-zero.
01629 */
01630 FuncDef *sqliteFindFunction(
01631   sqlite *db,        /* An open database */
01632   const char *zName, /* Name of the function.  Not null-terminated */
01633   int nName,         /* Number of characters in the name */
01634   int nArg,          /* Number of arguments.  -1 means any number */
01635   int createFlag     /* Create new entry if true and does not otherwise exist */
01636 ){
01637   FuncDef *pFirst, *p, *pMaybe;
01638   pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
01639   if( p && !createFlag && nArg<0 ){
01640     while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
01641     return p;
01642   }
01643   pMaybe = 0;
01644   while( p && p->nArg!=nArg ){
01645     if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
01646     p = p->pNext;
01647   }
01648   if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
01649     return 0;
01650   }
01651   if( p==0 && pMaybe ){
01652     assert( createFlag==0 );
01653     return pMaybe;
01654   }
01655   if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
01656     p->nArg = nArg;
01657     p->pNext = pFirst;
01658     p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
01659     sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
01660   }
01661   return p;
01662 }

Generated on Sun Dec 25 12:29:51 2005 for sqlite 2.8.17 by  doxygen 1.4.2