4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This file contains routines used for walking the parser tree for
15 #include "sqliteInt.h"
20 #if !defined(SQLITE_OMIT_WINDOWFUNC)
22 ** Walk all expressions linked into the list of Window objects passed
23 ** as the second argument.
25 static int walkWindowList(Walker
*pWalker
, Window
*pList
, int bOneOnly
){
27 for(pWin
=pList
; pWin
; pWin
=pWin
->pNextWin
){
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
;
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.)
50 ** The return value from the callback should be one of the WRC_*
51 ** constants to specify how to proceed with the walk.
53 ** WRC_Continue Continue descending down the tree.
55 ** WRC_Prune Do not descend into child nodes, but allow
56 ** the walk to continue with sibling nodes.
58 ** WRC_Abort Do no more callbacks. Unwind the stack and
59 ** return from the top-level walk call.
61 ** The return value from this routine is WRC_Abort to abandon the tree walk
62 ** and WRC_Continue to continue.
64 SQLITE_NOINLINE
int sqlite3WalkExprNN(Walker
*pWalker
, Expr
*pExpr
){
66 testcase( ExprHasProperty(pExpr
, EP_TokenOnly
) );
67 testcase( ExprHasProperty(pExpr
, EP_Reduced
) );
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
&& sqlite3WalkExprNN(pWalker
, pExpr
->pLeft
) ){
77 assert( !ExprHasProperty(pExpr
, EP_WinFunc
) );
78 pExpr
= pExpr
->pRight
;
80 }else if( ExprUseXSelect(pExpr
) ){
81 assert( !ExprHasProperty(pExpr
, EP_WinFunc
) );
82 if( sqlite3WalkSelect(pWalker
, pExpr
->x
.pSelect
) ) return WRC_Abort
;
85 if( sqlite3WalkExprList(pWalker
, pExpr
->x
.pList
) ) return WRC_Abort
;
87 #ifndef SQLITE_OMIT_WINDOWFUNC
88 if( ExprHasProperty(pExpr
, EP_WinFunc
) ){
89 if( walkWindowList(pWalker
, pExpr
->y
.pWin
, 1) ) return WRC_Abort
;
98 int sqlite3WalkExpr(Walker
*pWalker
, Expr
*pExpr
){
99 return pExpr
? sqlite3WalkExprNN(pWalker
,pExpr
) : WRC_Continue
;
103 ** Call sqlite3WalkExpr() for every expression in list p or until
104 ** an abort request is seen.
106 int sqlite3WalkExprList(Walker
*pWalker
, ExprList
*p
){
108 struct ExprList_item
*pItem
;
110 for(i
=p
->nExpr
, pItem
=p
->a
; i
>0; i
--, pItem
++){
111 if( sqlite3WalkExpr(pWalker
, pItem
->pExpr
) ) return WRC_Abort
;
118 ** This is a no-op callback for Walker->xSelectCallback2. If this
119 ** callback is set, then the Select->pWinDefn list is traversed.
121 void sqlite3WalkWinDefnDummyCallback(Walker
*pWalker
, Select
*p
){
122 UNUSED_PARAMETER(pWalker
);
128 ** Walk all expressions associated with SELECT statement p. Do
129 ** not invoke the SELECT callback on p, but do (of course) invoke
130 ** any expr callbacks and SELECT callbacks that come from subqueries.
131 ** Return WRC_Abort or WRC_Continue.
133 int sqlite3WalkSelectExpr(Walker
*pWalker
, Select
*p
){
134 if( sqlite3WalkExprList(pWalker
, p
->pEList
) ) return WRC_Abort
;
135 if( sqlite3WalkExpr(pWalker
, p
->pWhere
) ) return WRC_Abort
;
136 if( sqlite3WalkExprList(pWalker
, p
->pGroupBy
) ) return WRC_Abort
;
137 if( sqlite3WalkExpr(pWalker
, p
->pHaving
) ) return WRC_Abort
;
138 if( sqlite3WalkExprList(pWalker
, p
->pOrderBy
) ) return WRC_Abort
;
139 if( sqlite3WalkExpr(pWalker
, p
->pLimit
) ) return WRC_Abort
;
140 #if !defined(SQLITE_OMIT_WINDOWFUNC)
143 if( pWalker
->xSelectCallback2
==sqlite3WalkWinDefnDummyCallback
144 || ((pParse
= pWalker
->pParse
)!=0 && IN_RENAME_OBJECT
)
145 #ifndef SQLITE_OMIT_CTE
146 || pWalker
->xSelectCallback2
==sqlite3SelectPopWith
149 /* The following may return WRC_Abort if there are unresolvable
150 ** symbols (e.g. a table that does not exist) in a window definition. */
151 int rc
= walkWindowList(pWalker
, p
->pWinDefn
, 0);
160 ** Walk the parse trees associated with all subqueries in the
161 ** FROM clause of SELECT statement p. Do not invoke the select
162 ** callback on p, but do invoke it on each FROM clause subquery
163 ** and on any subqueries further down in the tree. Return
164 ** WRC_Abort or WRC_Continue;
166 int sqlite3WalkSelectFrom(Walker
*pWalker
, Select
*p
){
173 for(i
=pSrc
->nSrc
, pItem
=pSrc
->a
; i
>0; i
--, pItem
++){
174 if( pItem
->pSelect
&& sqlite3WalkSelect(pWalker
, pItem
->pSelect
) ){
177 if( pItem
->fg
.isTabFunc
178 && sqlite3WalkExprList(pWalker
, pItem
->u1
.pFuncArg
)
188 ** Call sqlite3WalkExpr() for every expression in Select statement p.
189 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
190 ** on the compound select chain, p->pPrior.
192 ** If it is not NULL, the xSelectCallback() callback is invoked before
193 ** the walk of the expressions and FROM clause. The xSelectCallback2()
194 ** method is invoked following the walk of the expressions and FROM clause,
195 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
196 ** and if the expressions and FROM clause both return WRC_Continue;
198 ** Return WRC_Continue under normal conditions. Return WRC_Abort if
199 ** there is an abort request.
201 ** If the Walker does not have an xSelectCallback() then this routine
202 ** is a no-op returning WRC_Continue.
204 int sqlite3WalkSelect(Walker
*pWalker
, Select
*p
){
206 if( p
==0 ) return WRC_Continue
;
207 if( pWalker
->xSelectCallback
==0 ) return WRC_Continue
;
209 rc
= pWalker
->xSelectCallback(pWalker
, p
);
210 if( rc
) return rc
& WRC_Abort
;
211 if( sqlite3WalkSelectExpr(pWalker
, p
)
212 || sqlite3WalkSelectFrom(pWalker
, p
)
216 if( pWalker
->xSelectCallback2
){
217 pWalker
->xSelectCallback2(pWalker
, p
);
224 /* Increase the walkerDepth when entering a subquery, and
225 ** decrease when leaving the subquery.
227 int sqlite3WalkerDepthIncrease(Walker
*pWalker
, Select
*pSelect
){
228 UNUSED_PARAMETER(pSelect
);
229 pWalker
->walkerDepth
++;
232 void sqlite3WalkerDepthDecrease(Walker
*pWalker
, Select
*pSelect
){
233 UNUSED_PARAMETER(pSelect
);
234 pWalker
->walkerDepth
--;
239 ** No-op routine for the parse-tree walker.
241 ** When this routine is the Walker.xExprCallback then expression trees
242 ** are walked without any actions being taken at each node. Presumably,
243 ** when this routine is used for Walker.xExprCallback then
244 ** Walker.xSelectCallback is set to do something useful for every
245 ** subquery in the parser tree.
247 int sqlite3ExprWalkNoop(Walker
*NotUsed
, Expr
*NotUsed2
){
248 UNUSED_PARAMETER2(NotUsed
, NotUsed2
);
253 ** No-op routine for the parse-tree walker for SELECT statements.
254 ** subquery in the parser tree.
256 int sqlite3SelectWalkNoop(Walker
*NotUsed
, Select
*NotUsed2
){
257 UNUSED_PARAMETER2(NotUsed
, NotUsed2
);