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