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, int bOneOnly){ 26 Window *pWin; 27 for(pWin=pList; pWin; pWin=pWin->pNextWin){ 28 int rc; 29 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy); 30 if( rc ) return WRC_Abort; 31 rc = sqlite3WalkExprList(pWalker, pWin->pPartition); 32 if( rc ) return WRC_Abort; 33 rc = sqlite3WalkExpr(pWalker, pWin->pFilter); 34 if( rc ) return WRC_Abort; 35 rc = sqlite3WalkExpr(pWalker, pWin->pStart); 36 if( rc ) return WRC_Abort; 37 rc = sqlite3WalkExpr(pWalker, pWin->pEnd); 38 if( rc ) return WRC_Abort; 39 if( bOneOnly ) break; 40 } 41 return WRC_Continue; 42 } 43 #endif 44 45 /* 46 ** Walk an expression tree. Invoke the callback once for each node 47 ** of the expression, while descending. (In other words, the callback 48 ** is invoked before visiting children.) 49 ** 50 ** The return value from the callback should be one of the WRC_* 51 ** constants to specify how to proceed with the walk. 52 ** 53 ** WRC_Continue Continue descending down the tree. 54 ** 55 ** WRC_Prune Do not descend into child nodes, but allow 56 ** the walk to continue with sibling nodes. 57 ** 58 ** WRC_Abort Do no more callbacks. Unwind the stack and 59 ** return from the top-level walk call. 60 ** 61 ** The return value from this routine is WRC_Abort to abandon the tree walk 62 ** and WRC_Continue to continue. 63 */ 64 static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){ 65 int rc; 66 testcase( ExprHasProperty(pExpr, EP_TokenOnly) ); 67 testcase( ExprHasProperty(pExpr, EP_Reduced) ); 68 while(1){ 69 rc = pWalker->xExprCallback(pWalker, pExpr); 70 if( rc ) return rc & WRC_Abort; 71 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){ 72 assert( pExpr->x.pList==0 || pExpr->pRight==0 ); 73 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort; 74 if( pExpr->pRight ){ 75 assert( !ExprHasProperty(pExpr, EP_WinFunc) ); 76 pExpr = pExpr->pRight; 77 continue; 78 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 79 assert( !ExprHasProperty(pExpr, EP_WinFunc) ); 80 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; 81 }else{ 82 if( pExpr->x.pList ){ 83 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; 84 } 85 #ifndef SQLITE_OMIT_WINDOWFUNC 86 if( ExprHasProperty(pExpr, EP_WinFunc) ){ 87 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort; 88 } 89 #endif 90 } 91 } 92 break; 93 } 94 return WRC_Continue; 95 } 96 int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){ 97 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue; 98 } 99 100 /* 101 ** Call sqlite3WalkExpr() for every expression in list p or until 102 ** an abort request is seen. 103 */ 104 int sqlite3WalkExprList(Walker *pWalker, ExprList *p){ 105 int i; 106 struct ExprList_item *pItem; 107 if( p ){ 108 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ 109 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort; 110 } 111 } 112 return WRC_Continue; 113 } 114 115 /* 116 ** Walk all expressions associated with SELECT statement p. Do 117 ** not invoke the SELECT callback on p, but do (of course) invoke 118 ** any expr callbacks and SELECT callbacks that come from subqueries. 119 ** Return WRC_Abort or WRC_Continue. 120 */ 121 int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){ 122 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort; 123 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort; 124 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort; 125 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort; 126 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort; 127 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort; 128 #if !defined(SQLITE_OMIT_WINDOWFUNC) 129 if( p->pWinDefn ){ 130 Parse *pParse; 131 if( pWalker->bWalkWinDefn 132 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT) 133 ){ 134 /* The following may return WRC_Abort if there are unresolvable 135 ** symbols (e.g. a table that does not exist) in a window definition. */ 136 int rc = walkWindowList(pWalker, p->pWinDefn, 0); 137 return rc; 138 } 139 } 140 #endif 141 return WRC_Continue; 142 } 143 144 /* 145 ** Walk the parse trees associated with all subqueries in the 146 ** FROM clause of SELECT statement p. Do not invoke the select 147 ** callback on p, but do invoke it on each FROM clause subquery 148 ** and on any subqueries further down in the tree. Return 149 ** WRC_Abort or WRC_Continue; 150 */ 151 int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ 152 SrcList *pSrc; 153 int i; 154 SrcItem *pItem; 155 156 pSrc = p->pSrc; 157 if( pSrc ){ 158 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ 159 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){ 160 return WRC_Abort; 161 } 162 if( pItem->fg.isTabFunc 163 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg) 164 ){ 165 return WRC_Abort; 166 } 167 } 168 } 169 return WRC_Continue; 170 } 171 172 /* 173 ** Call sqlite3WalkExpr() for every expression in Select statement p. 174 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and 175 ** on the compound select chain, p->pPrior. 176 ** 177 ** If it is not NULL, the xSelectCallback() callback is invoked before 178 ** the walk of the expressions and FROM clause. The xSelectCallback2() 179 ** method is invoked following the walk of the expressions and FROM clause, 180 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL 181 ** and if the expressions and FROM clause both return WRC_Continue; 182 ** 183 ** Return WRC_Continue under normal conditions. Return WRC_Abort if 184 ** there is an abort request. 185 ** 186 ** If the Walker does not have an xSelectCallback() then this routine 187 ** is a no-op returning WRC_Continue. 188 */ 189 int sqlite3WalkSelect(Walker *pWalker, Select *p){ 190 int rc; 191 if( p==0 ) return WRC_Continue; 192 if( pWalker->xSelectCallback==0 ) return WRC_Continue; 193 do{ 194 rc = pWalker->xSelectCallback(pWalker, p); 195 if( rc ) return rc & WRC_Abort; 196 if( sqlite3WalkSelectExpr(pWalker, p) 197 || sqlite3WalkSelectFrom(pWalker, p) 198 ){ 199 return WRC_Abort; 200 } 201 if( pWalker->xSelectCallback2 ){ 202 pWalker->xSelectCallback2(pWalker, p); 203 } 204 p = p->pPrior; 205 }while( p!=0 ); 206 return WRC_Continue; 207 } 208 209 /* Increase the walkerDepth when entering a subquery, and 210 ** descrease when leaving the subquery. 211 */ 212 int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){ 213 UNUSED_PARAMETER(pSelect); 214 pWalker->walkerDepth++; 215 return WRC_Continue; 216 } 217 void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){ 218 UNUSED_PARAMETER(pSelect); 219 pWalker->walkerDepth--; 220 } 221 222 223 /* 224 ** No-op routine for the parse-tree walker. 225 ** 226 ** When this routine is the Walker.xExprCallback then expression trees 227 ** are walked without any actions being taken at each node. Presumably, 228 ** when this routine is used for Walker.xExprCallback then 229 ** Walker.xSelectCallback is set to do something useful for every 230 ** subquery in the parser tree. 231 */ 232 int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ 233 UNUSED_PARAMETER2(NotUsed, NotUsed2); 234 return WRC_Continue; 235 } 236 237 /* 238 ** No-op routine for the parse-tree walker for SELECT statements. 239 ** subquery in the parser tree. 240 */ 241 int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){ 242 UNUSED_PARAMETER2(NotUsed, NotUsed2); 243 return WRC_Continue; 244 } 245