| OLD | NEW | 
 | (Empty) | 
|    1 /* |  | 
|    2 ** 2008 August 16 |  | 
|    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 walking the parser tree for |  | 
|   13 ** an SQL statement. |  | 
|   14 ** |  | 
|   15 ** $Id: walker.c,v 1.7 2009/06/15 23:15:59 drh Exp $ |  | 
|   16 */ |  | 
|   17 #include "sqliteInt.h" |  | 
|   18 #include <stdlib.h> |  | 
|   19 #include <string.h> |  | 
|   20  |  | 
|   21  |  | 
|   22 /* |  | 
|   23 ** Walk an expression tree.  Invoke the callback once for each node |  | 
|   24 ** of the expression, while decending.  (In other words, the callback |  | 
|   25 ** is invoked before visiting children.) |  | 
|   26 ** |  | 
|   27 ** The return value from the callback should be one of the WRC_* |  | 
|   28 ** constants to specify how to proceed with the walk. |  | 
|   29 ** |  | 
|   30 **    WRC_Continue      Continue descending down the tree. |  | 
|   31 ** |  | 
|   32 **    WRC_Prune         Do not descend into child nodes.  But allow |  | 
|   33 **                      the walk to continue with sibling nodes. |  | 
|   34 ** |  | 
|   35 **    WRC_Abort         Do no more callbacks.  Unwind the stack and |  | 
|   36 **                      return the top-level walk call. |  | 
|   37 ** |  | 
|   38 ** The return value from this routine is WRC_Abort to abandon the tree walk |  | 
|   39 ** and WRC_Continue to continue. |  | 
|   40 */ |  | 
|   41 int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){ |  | 
|   42   int rc; |  | 
|   43   if( pExpr==0 ) return WRC_Continue; |  | 
|   44   testcase( ExprHasProperty(pExpr, EP_TokenOnly) ); |  | 
|   45   testcase( ExprHasProperty(pExpr, EP_Reduced) ); |  | 
|   46   rc = pWalker->xExprCallback(pWalker, pExpr); |  | 
|   47   if( rc==WRC_Continue |  | 
|   48               && !ExprHasAnyProperty(pExpr,EP_TokenOnly) ){ |  | 
|   49     if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort; |  | 
|   50     if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort; |  | 
|   51     if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |  | 
|   52       if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; |  | 
|   53     }else{ |  | 
|   54       if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; |  | 
|   55     } |  | 
|   56   } |  | 
|   57   return rc & WRC_Abort; |  | 
|   58 } |  | 
|   59  |  | 
|   60 /* |  | 
|   61 ** Call sqlite3WalkExpr() for every expression in list p or until |  | 
|   62 ** an abort request is seen. |  | 
|   63 */ |  | 
|   64 int sqlite3WalkExprList(Walker *pWalker, ExprList *p){ |  | 
|   65   int i; |  | 
|   66   struct ExprList_item *pItem; |  | 
|   67   if( p ){ |  | 
|   68     for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ |  | 
|   69       if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort; |  | 
|   70     } |  | 
|   71   } |  | 
|   72   return WRC_Continue; |  | 
|   73 } |  | 
|   74  |  | 
|   75 /* |  | 
|   76 ** Walk all expressions associated with SELECT statement p.  Do |  | 
|   77 ** not invoke the SELECT callback on p, but do (of course) invoke |  | 
|   78 ** any expr callbacks and SELECT callbacks that come from subqueries. |  | 
|   79 ** Return WRC_Abort or WRC_Continue. |  | 
|   80 */ |  | 
|   81 int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){ |  | 
|   82   if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort; |  | 
|   83   if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort; |  | 
|   84   if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort; |  | 
|   85   if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort; |  | 
|   86   if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort; |  | 
|   87   if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort; |  | 
|   88   if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort; |  | 
|   89   return WRC_Continue; |  | 
|   90 } |  | 
|   91  |  | 
|   92 /* |  | 
|   93 ** Walk the parse trees associated with all subqueries in the |  | 
|   94 ** FROM clause of SELECT statement p.  Do not invoke the select |  | 
|   95 ** callback on p, but do invoke it on each FROM clause subquery |  | 
|   96 ** and on any subqueries further down in the tree.  Return  |  | 
|   97 ** WRC_Abort or WRC_Continue; |  | 
|   98 */ |  | 
|   99 int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ |  | 
|  100   SrcList *pSrc; |  | 
|  101   int i; |  | 
|  102   struct SrcList_item *pItem; |  | 
|  103  |  | 
|  104   pSrc = p->pSrc; |  | 
|  105   if( ALWAYS(pSrc) ){ |  | 
|  106     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ |  | 
|  107       if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){ |  | 
|  108         return WRC_Abort; |  | 
|  109       } |  | 
|  110     } |  | 
|  111   } |  | 
|  112   return WRC_Continue; |  | 
|  113 }  |  | 
|  114  |  | 
|  115 /* |  | 
|  116 ** Call sqlite3WalkExpr() for every expression in Select statement p. |  | 
|  117 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and |  | 
|  118 ** on the compound select chain, p->pPrior. |  | 
|  119 ** |  | 
|  120 ** Return WRC_Continue under normal conditions.  Return WRC_Abort if |  | 
|  121 ** there is an abort request. |  | 
|  122 ** |  | 
|  123 ** If the Walker does not have an xSelectCallback() then this routine |  | 
|  124 ** is a no-op returning WRC_Continue. |  | 
|  125 */ |  | 
|  126 int sqlite3WalkSelect(Walker *pWalker, Select *p){ |  | 
|  127   int rc; |  | 
|  128   if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue; |  | 
|  129   rc = WRC_Continue; |  | 
|  130   while( p  ){ |  | 
|  131     rc = pWalker->xSelectCallback(pWalker, p); |  | 
|  132     if( rc ) break; |  | 
|  133     if( sqlite3WalkSelectExpr(pWalker, p) ) return WRC_Abort; |  | 
|  134     if( sqlite3WalkSelectFrom(pWalker, p) ) return WRC_Abort; |  | 
|  135     p = p->pPrior; |  | 
|  136   } |  | 
|  137   return rc & WRC_Abort; |  | 
|  138 } |  | 
| OLD | NEW |