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"
21 ** Walk an expression tree. Invoke the callback once for each node
22 ** of the expression, while descending. (In other words, the callback
23 ** is invoked before visiting children.)
25 ** The return value from the callback should be one of the WRC_*
26 ** constants to specify how to proceed with the walk.
28 ** WRC_Continue Continue descending down the tree.
30 ** WRC_Prune Do not descend into child nodes, but allow
31 ** the walk to continue with sibling nodes.
33 ** WRC_Abort Do no more callbacks. Unwind the stack and
34 ** return from the top-level walk call.
36 ** The return value from this routine is WRC_Abort to abandon the tree walk
37 ** and WRC_Continue to continue.
39 static SQLITE_NOINLINE
int walkExpr(Walker
*pWalker
, Expr
*pExpr
){
41 testcase( ExprHasProperty(pExpr
, EP_TokenOnly
) );
42 testcase( ExprHasProperty(pExpr
, EP_Reduced
) );
44 rc
= pWalker
->xExprCallback(pWalker
, pExpr
);
45 if( rc
) return rc
& WRC_Abort
;
46 if( !ExprHasProperty(pExpr
,(EP_TokenOnly
|EP_Leaf
)) ){
47 if( pExpr
->pLeft
&& walkExpr(pWalker
, pExpr
->pLeft
) ) return WRC_Abort
;
48 assert( pExpr
->x
.pList
==0 || pExpr
->pRight
==0 );
50 pExpr
= pExpr
->pRight
;
52 }else if( ExprHasProperty(pExpr
, EP_xIsSelect
) ){
53 if( sqlite3WalkSelect(pWalker
, pExpr
->x
.pSelect
) ) return WRC_Abort
;
54 }else if( pExpr
->x
.pList
){
55 if( sqlite3WalkExprList(pWalker
, pExpr
->x
.pList
) ) return WRC_Abort
;
57 #ifndef SQLITE_OMIT_WINDOWFUNC
58 if( !ExprHasProperty(pExpr
, EP_Reduced
) && pExpr
->pWin
){
59 Window
*pWin
= pExpr
->pWin
;
60 if( sqlite3WalkExprList(pWalker
, pWin
->pPartition
) ) return WRC_Abort
;
61 if( sqlite3WalkExprList(pWalker
, pWin
->pOrderBy
) ) return WRC_Abort
;
62 if( sqlite3WalkExpr(pWalker
, pWin
->pFilter
) ) return WRC_Abort
;
70 int sqlite3WalkExpr(Walker
*pWalker
, Expr
*pExpr
){
71 return pExpr
? walkExpr(pWalker
,pExpr
) : WRC_Continue
;
75 ** Call sqlite3WalkExpr() for every expression in list p or until
76 ** an abort request is seen.
78 int sqlite3WalkExprList(Walker
*pWalker
, ExprList
*p
){
80 struct ExprList_item
*pItem
;
82 for(i
=p
->nExpr
, pItem
=p
->a
; i
>0; i
--, pItem
++){
83 if( sqlite3WalkExpr(pWalker
, pItem
->pExpr
) ) return WRC_Abort
;
90 ** Walk all expressions associated with SELECT statement p. Do
91 ** not invoke the SELECT callback on p, but do (of course) invoke
92 ** any expr callbacks and SELECT callbacks that come from subqueries.
93 ** Return WRC_Abort or WRC_Continue.
95 int sqlite3WalkSelectExpr(Walker
*pWalker
, Select
*p
){
96 if( sqlite3WalkExprList(pWalker
, p
->pEList
) ) return WRC_Abort
;
97 if( sqlite3WalkExpr(pWalker
, p
->pWhere
) ) return WRC_Abort
;
98 if( sqlite3WalkExprList(pWalker
, p
->pGroupBy
) ) return WRC_Abort
;
99 if( sqlite3WalkExpr(pWalker
, p
->pHaving
) ) return WRC_Abort
;
100 if( sqlite3WalkExprList(pWalker
, p
->pOrderBy
) ) return WRC_Abort
;
101 if( sqlite3WalkExpr(pWalker
, p
->pLimit
) ) return WRC_Abort
;
106 ** Walk the parse trees associated with all subqueries in the
107 ** FROM clause of SELECT statement p. Do not invoke the select
108 ** callback on p, but do invoke it on each FROM clause subquery
109 ** and on any subqueries further down in the tree. Return
110 ** WRC_Abort or WRC_Continue;
112 int sqlite3WalkSelectFrom(Walker
*pWalker
, Select
*p
){
115 struct SrcList_item
*pItem
;
119 for(i
=pSrc
->nSrc
, pItem
=pSrc
->a
; i
>0; i
--, pItem
++){
120 if( pItem
->pSelect
&& sqlite3WalkSelect(pWalker
, pItem
->pSelect
) ){
123 if( pItem
->fg
.isTabFunc
124 && sqlite3WalkExprList(pWalker
, pItem
->u1
.pFuncArg
)
133 ** Call sqlite3WalkExpr() for every expression in Select statement p.
134 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
135 ** on the compound select chain, p->pPrior.
137 ** If it is not NULL, the xSelectCallback() callback is invoked before
138 ** the walk of the expressions and FROM clause. The xSelectCallback2()
139 ** method is invoked following the walk of the expressions and FROM clause,
140 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
141 ** and if the expressions and FROM clause both return WRC_Continue;
143 ** Return WRC_Continue under normal conditions. Return WRC_Abort if
144 ** there is an abort request.
146 ** If the Walker does not have an xSelectCallback() then this routine
147 ** is a no-op returning WRC_Continue.
149 int sqlite3WalkSelect(Walker
*pWalker
, Select
*p
){
151 if( p
==0 ) return WRC_Continue
;
152 if( pWalker
->xSelectCallback
==0 ) return WRC_Continue
;
154 rc
= pWalker
->xSelectCallback(pWalker
, p
);
155 if( rc
) return rc
& WRC_Abort
;
156 if( sqlite3WalkSelectExpr(pWalker
, p
)
157 || sqlite3WalkSelectFrom(pWalker
, p
)
161 if( pWalker
->xSelectCallback2
){
162 pWalker
->xSelectCallback2(pWalker
, p
);