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 #include "sqliteInt.h" 16 #include <stdlib.h> 17 #include <string.h> 18 19 20 #if !defined(SQLITE_OMIT_WINDOWFUNC) 21 /* 22 ** Walk all expressions linked into the list of Window objects passed 23 ** as the second argument. 24 */ 25 static int walkWindowList(Walker *pWalker, Window *pList){ 26 Window *pWin; 27 for(pWin=pList; pWin; pWin=pWin->pNextWin){ 28 if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort; 29 if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort; 30 if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort; 31 } 32 return WRC_Continue; 33 } 34 #endif 35 36 /* 37 ** Walk an expression tree. Invoke the callback once for each node 38 ** of the expression, while descending. (In other words, the callback 39 ** is invoked before visiting children.) 40 ** 41 ** The return value from the callback should be one of the WRC_* 42 ** constants to specify how to proceed with the walk. 43 ** 44 ** WRC_Continue Continue descending down the tree. 45 ** 46 ** WRC_Prune Do not descend into child nodes, but allow 47 ** the walk to continue with sibling nodes. 48 ** 49 ** WRC_Abort Do no more callbacks. Unwind the stack and 50 ** return from the top-level walk call. 51 ** 52 ** The return value from this routine is WRC_Abort to abandon the tree walk 53 ** and WRC_Continue to continue. 54 */ 55 static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){ 56 int rc; 57 testcase( ExprHasProperty(pExpr, EP_TokenOnly) ); 58 testcase( ExprHasProperty(pExpr, EP_Reduced) ); 59 while(1){ 60 rc = pWalker->xExprCallback(pWalker, pExpr); 61 if( rc ) return rc & WRC_Abort; 62 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){ 63 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort; 64 assert( pExpr->x.pList==0 || pExpr->pRight==0 ); 65 if( pExpr->pRight ){ 66 assert( !ExprHasProperty(pExpr, EP_WinFunc|EP_Filter) ); 67 pExpr = pExpr->pRight; 68 continue; 69 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 70 assert( !ExprHasProperty(pExpr, EP_WinFunc|EP_Filter) ); 71 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; 72 }else{ 73 if( pExpr->x.pList ){ 74 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; 75 } 76 #ifndef SQLITE_OMIT_WINDOWFUNC 77 if( ExprHasProperty(pExpr, EP_WinFunc|EP_Filter) ){ 78 if( ExprHasProperty(pExpr, EP_WinFunc) ){ 79 if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort; 80 }else if( ExprHasProperty(pExpr, EP_Filter) ){ 81 if( walkExpr(pWalker, pExpr->y.pFilter) ) return WRC_Abort; 82 } 83 } 84 #endif 85 } 86 } 87 break; 88 } 89 return WRC_Continue; 90 } 91 int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){ 92 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue; 93 } 94 95 /* 96 ** Call sqlite3WalkExpr() for every expression in list p or until 97 ** an abort request is seen. 98 */ 99 int sqlite3WalkExprList(Walker *pWalker, ExprList *p){ 100 int i; 101 struct ExprList_item *pItem; 102 if( p ){ 103 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ 104 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort; 105 } 106 } 107 return WRC_Continue; 108 } 109 110 /* 111 ** Walk all expressions associated with SELECT statement p. Do 112 ** not invoke the SELECT callback on p, but do (of course) invoke 113 ** any expr callbacks and SELECT callbacks that come from subqueries. 114 ** Return WRC_Abort or WRC_Continue. 115 */ 116 int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){ 117 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort; 118 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort; 119 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort; 120 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort; 121 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort; 122 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort; 123 #if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE) 124 { 125 Parse *pParse = pWalker->pParse; 126 if( pParse && IN_RENAME_OBJECT ){ 127 int rc = walkWindowList(pWalker, p->pWinDefn); 128 assert( rc==WRC_Continue ); 129 return rc; 130 } 131 } 132 #endif 133 return WRC_Continue; 134 } 135 136 /* 137 ** Walk the parse trees associated with all subqueries in the 138 ** FROM clause of SELECT statement p. Do not invoke the select 139 ** callback on p, but do invoke it on each FROM clause subquery 140 ** and on any subqueries further down in the tree. Return 141 ** WRC_Abort or WRC_Continue; 142 */ 143 int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ 144 SrcList *pSrc; 145 int i; 146 struct SrcList_item *pItem; 147 148 pSrc = p->pSrc; 149 assert( pSrc!=0 ); 150 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ 151 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){ 152 return WRC_Abort; 153 } 154 if( pItem->fg.isTabFunc 155 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg) 156 ){ 157 return WRC_Abort; 158 } 159 } 160 return WRC_Continue; 161 } 162 163 /* 164 ** Call sqlite3WalkExpr() for every expression in Select statement p. 165 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and 166 ** on the compound select chain, p->pPrior. 167 ** 168 ** If it is not NULL, the xSelectCallback() callback is invoked before 169 ** the walk of the expressions and FROM clause. The xSelectCallback2() 170 ** method is invoked following the walk of the expressions and FROM clause, 171 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL 172 ** and if the expressions and FROM clause both return WRC_Continue; 173 ** 174 ** Return WRC_Continue under normal conditions. Return WRC_Abort if 175 ** there is an abort request. 176 ** 177 ** If the Walker does not have an xSelectCallback() then this routine 178 ** is a no-op returning WRC_Continue. 179 */ 180 int sqlite3WalkSelect(Walker *pWalker, Select *p){ 181 int rc; 182 if( p==0 ) return WRC_Continue; 183 if( pWalker->xSelectCallback==0 ) return WRC_Continue; 184 do{ 185 rc = pWalker->xSelectCallback(pWalker, p); 186 if( rc ) return rc & WRC_Abort; 187 if( sqlite3WalkSelectExpr(pWalker, p) 188 || sqlite3WalkSelectFrom(pWalker, p) 189 ){ 190 return WRC_Abort; 191 } 192 if( pWalker->xSelectCallback2 ){ 193 pWalker->xSelectCallback2(pWalker, p); 194 } 195 p = p->pPrior; 196 }while( p!=0 ); 197 return WRC_Continue; 198 } 199