drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 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. |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 14 | */ |
| 15 | #include "sqliteInt.h" |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | |
| 20 | /* |
| 21 | ** Walk an expression tree. Invoke the callback once for each node |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 22 | ** of the expression, while descending. (In other words, the callback |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 23 | ** is invoked before visiting children.) |
| 24 | ** |
| 25 | ** The return value from the callback should be one of the WRC_* |
| 26 | ** constants to specify how to proceed with the walk. |
| 27 | ** |
| 28 | ** WRC_Continue Continue descending down the tree. |
| 29 | ** |
drh | 567bd44 | 2017-03-07 12:18:23 +0000 | [diff] [blame] | 30 | ** WRC_Prune Do not descend into child nodes, but allow |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 31 | ** the walk to continue with sibling nodes. |
| 32 | ** |
| 33 | ** WRC_Abort Do no more callbacks. Unwind the stack and |
drh | 567bd44 | 2017-03-07 12:18:23 +0000 | [diff] [blame] | 34 | ** return from the top-level walk call. |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 35 | ** |
| 36 | ** The return value from this routine is WRC_Abort to abandon the tree walk |
| 37 | ** and WRC_Continue to continue. |
| 38 | */ |
drh | 50922cf | 2016-01-11 14:19:14 +0000 | [diff] [blame] | 39 | static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 40 | int rc; |
drh | 0a9aa22 | 2009-04-08 12:21:30 +0000 | [diff] [blame] | 41 | testcase( ExprHasProperty(pExpr, EP_TokenOnly) ); |
drh | 0a9aa22 | 2009-04-08 12:21:30 +0000 | [diff] [blame] | 42 | testcase( ExprHasProperty(pExpr, EP_Reduced) ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 43 | rc = pWalker->xExprCallback(pWalker, pExpr); |
drh | c2d14a9 | 2017-07-07 12:58:30 +0000 | [diff] [blame] | 44 | if( rc ) return rc & WRC_Abort; |
| 45 | if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){ |
| 46 | if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort; |
drh | d108667 | 2017-07-07 13:59:34 +0000 | [diff] [blame] | 47 | assert( pExpr->x.pList==0 || pExpr->pRight==0 ); |
| 48 | if( pExpr->pRight ){ |
| 49 | if( walkExpr(pWalker, pExpr->pRight) ) return WRC_Abort; |
| 50 | }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
drh | c2d14a9 | 2017-07-07 12:58:30 +0000 | [diff] [blame] | 51 | if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; |
| 52 | }else if( pExpr->x.pList ){ |
| 53 | if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; |
| 54 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 55 | } |
drh | 922802c | 2016-08-10 18:56:32 +0000 | [diff] [blame] | 56 | return WRC_Continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 57 | } |
drh | 50922cf | 2016-01-11 14:19:14 +0000 | [diff] [blame] | 58 | int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){ |
| 59 | return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue; |
| 60 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | ** Call sqlite3WalkExpr() for every expression in list p or until |
| 64 | ** an abort request is seen. |
| 65 | */ |
| 66 | int sqlite3WalkExprList(Walker *pWalker, ExprList *p){ |
drh | f7828b5 | 2009-06-15 23:15:59 +0000 | [diff] [blame] | 67 | int i; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 68 | struct ExprList_item *pItem; |
| 69 | if( p ){ |
| 70 | for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ |
| 71 | if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort; |
| 72 | } |
| 73 | } |
drh | f7828b5 | 2009-06-15 23:15:59 +0000 | [diff] [blame] | 74 | return WRC_Continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* |
| 78 | ** Walk all expressions associated with SELECT statement p. Do |
| 79 | ** not invoke the SELECT callback on p, but do (of course) invoke |
| 80 | ** any expr callbacks and SELECT callbacks that come from subqueries. |
| 81 | ** Return WRC_Abort or WRC_Continue. |
| 82 | */ |
| 83 | int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){ |
| 84 | if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort; |
| 85 | if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort; |
| 86 | if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort; |
| 87 | if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort; |
| 88 | if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort; |
| 89 | if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort; |
| 90 | if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort; |
| 91 | return WRC_Continue; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | ** Walk the parse trees associated with all subqueries in the |
| 96 | ** FROM clause of SELECT statement p. Do not invoke the select |
| 97 | ** callback on p, but do invoke it on each FROM clause subquery |
| 98 | ** and on any subqueries further down in the tree. Return |
| 99 | ** WRC_Abort or WRC_Continue; |
| 100 | */ |
| 101 | int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ |
| 102 | SrcList *pSrc; |
| 103 | int i; |
| 104 | struct SrcList_item *pItem; |
| 105 | |
| 106 | pSrc = p->pSrc; |
drh | f7828b5 | 2009-06-15 23:15:59 +0000 | [diff] [blame] | 107 | if( ALWAYS(pSrc) ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 108 | for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ |
drh | 3da70a6 | 2017-07-07 11:49:48 +0000 | [diff] [blame] | 109 | if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 110 | return WRC_Abort; |
| 111 | } |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 112 | if( pItem->fg.isTabFunc |
| 113 | && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg) |
| 114 | ){ |
| 115 | return WRC_Abort; |
| 116 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return WRC_Continue; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | ** Call sqlite3WalkExpr() for every expression in Select statement p. |
| 124 | ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 125 | ** on the compound select chain, p->pPrior. |
| 126 | ** |
| 127 | ** If it is not NULL, the xSelectCallback() callback is invoked before |
| 128 | ** the walk of the expressions and FROM clause. The xSelectCallback2() |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 129 | ** method is invoked following the walk of the expressions and FROM clause, |
| 130 | ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL |
| 131 | ** and if the expressions and FROM clause both return WRC_Continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 132 | ** |
| 133 | ** Return WRC_Continue under normal conditions. Return WRC_Abort if |
| 134 | ** there is an abort request. |
| 135 | ** |
| 136 | ** If the Walker does not have an xSelectCallback() then this routine |
| 137 | ** is a no-op returning WRC_Continue. |
| 138 | */ |
| 139 | int sqlite3WalkSelect(Walker *pWalker, Select *p){ |
| 140 | int rc; |
drh | 3da70a6 | 2017-07-07 11:49:48 +0000 | [diff] [blame] | 141 | if( p==0 ) return WRC_Continue; |
| 142 | if( pWalker->xSelectCallback==0 ) return WRC_Continue; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 143 | do{ |
| 144 | rc = pWalker->xSelectCallback(pWalker, p); |
| 145 | if( rc ) return rc & WRC_Abort; |
drh | ed551b9 | 2012-08-23 19:46:11 +0000 | [diff] [blame] | 146 | if( sqlite3WalkSelectExpr(pWalker, p) |
| 147 | || sqlite3WalkSelectFrom(pWalker, p) |
| 148 | ){ |
drh | ed551b9 | 2012-08-23 19:46:11 +0000 | [diff] [blame] | 149 | return WRC_Abort; |
| 150 | } |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 151 | if( pWalker->xSelectCallback2 ){ |
| 152 | pWalker->xSelectCallback2(pWalker, p); |
drh | aa87f9a | 2013-04-25 00:57:10 +0000 | [diff] [blame] | 153 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 154 | p = p->pPrior; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 155 | }while( p!=0 ); |
| 156 | return WRC_Continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 157 | } |