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
){
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
;
37 ** Walk an expression tree. Invoke the callback once for each node
38 ** of the expression, while descending. (In other words, the callback
39 ** is invoked before visiting children.)
41 ** The return value from the callback should be one of the WRC_*
42 ** constants to specify how to proceed with the walk.
44 ** WRC_Continue Continue descending down the tree.
46 ** WRC_Prune Do not descend into child nodes, but allow
47 ** the walk to continue with sibling nodes.
49 ** WRC_Abort Do no more callbacks. Unwind the stack and
50 ** return from the top-level walk call.
52 ** The return value from this routine is WRC_Abort to abandon the tree walk
53 ** and WRC_Continue to continue.
55 static SQLITE_NOINLINE
int walkExpr(Walker
*pWalker
, Expr
*pExpr
){
57 testcase( ExprHasProperty(pExpr
, EP_TokenOnly
) );
58 testcase( ExprHasProperty(pExpr
, EP_Reduced
) );
60 rc
= pWalker
->xExprCallback(pWalker
, pExpr
);
61 if( rc
) return rc
& WRC_Abort
;
62 if( !ExprHasProperty(pExpr
,(EP_TokenOnly
|EP_Leaf
)) ){
63 if( pExpr
->pLeft
&& walkExpr(pWalker
, pExpr
->pLeft
) ) return WRC_Abort
;
64 assert( pExpr
->x
.pList
==0 || pExpr
->pRight
==0 );
66 pExpr
= pExpr
->pRight
;
68 }else if( ExprHasProperty(pExpr
, EP_xIsSelect
) ){
69 if( sqlite3WalkSelect(pWalker
, pExpr
->x
.pSelect
) ) return WRC_Abort
;
70 }else if( pExpr
->x
.pList
){
71 if( sqlite3WalkExprList(pWalker
, pExpr
->x
.pList
) ) return WRC_Abort
;
73 #ifndef SQLITE_OMIT_WINDOWFUNC
74 if( ExprHasProperty(pExpr
, EP_WinFunc
) ){
75 if( walkWindowList(pWalker
, pExpr
->y
.pWin
) ) return WRC_Abort
;
83 int sqlite3WalkExpr(Walker
*pWalker
, Expr
*pExpr
){
84 return pExpr
? walkExpr(pWalker
,pExpr
) : WRC_Continue
;
88 ** Call sqlite3WalkExpr() for every expression in list p or until
89 ** an abort request is seen.
91 int sqlite3WalkExprList(Walker
*pWalker
, ExprList
*p
){
93 struct ExprList_item
*pItem
;
95 for(i
=p
->nExpr
, pItem
=p
->a
; i
>0; i
--, pItem
++){
96 if( sqlite3WalkExpr(pWalker
, pItem
->pExpr
) ) return WRC_Abort
;
103 ** Walk all expressions associated with SELECT statement p. Do
104 ** not invoke the SELECT callback on p, but do (of course) invoke
105 ** any expr callbacks and SELECT callbacks that come from subqueries.
106 ** Return WRC_Abort or WRC_Continue.
108 int sqlite3WalkSelectExpr(Walker
*pWalker
, Select
*p
){
109 if( sqlite3WalkExprList(pWalker
, p
->pEList
) ) return WRC_Abort
;
110 if( sqlite3WalkExpr(pWalker
, p
->pWhere
) ) return WRC_Abort
;
111 if( sqlite3WalkExprList(pWalker
, p
->pGroupBy
) ) return WRC_Abort
;
112 if( sqlite3WalkExpr(pWalker
, p
->pHaving
) ) return WRC_Abort
;
113 if( sqlite3WalkExprList(pWalker
, p
->pOrderBy
) ) return WRC_Abort
;
114 if( sqlite3WalkExpr(pWalker
, p
->pLimit
) ) return WRC_Abort
;
115 #if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE)
117 Parse
*pParse
= pWalker
->pParse
;
118 if( pParse
&& IN_RENAME_OBJECT
){
119 int rc
= walkWindowList(pWalker
, p
->pWinDefn
);
120 assert( rc
==WRC_Continue
);
129 ** Walk the parse trees associated with all subqueries in the
130 ** FROM clause of SELECT statement p. Do not invoke the select
131 ** callback on p, but do invoke it on each FROM clause subquery
132 ** and on any subqueries further down in the tree. Return
133 ** WRC_Abort or WRC_Continue;
135 int sqlite3WalkSelectFrom(Walker
*pWalker
, Select
*p
){
138 struct SrcList_item
*pItem
;
142 for(i
=pSrc
->nSrc
, pItem
=pSrc
->a
; i
>0; i
--, pItem
++){
143 if( pItem
->pSelect
&& sqlite3WalkSelect(pWalker
, pItem
->pSelect
) ){
146 if( pItem
->fg
.isTabFunc
147 && sqlite3WalkExprList(pWalker
, pItem
->u1
.pFuncArg
)
156 ** Call sqlite3WalkExpr() for every expression in Select statement p.
157 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
158 ** on the compound select chain, p->pPrior.
160 ** If it is not NULL, the xSelectCallback() callback is invoked before
161 ** the walk of the expressions and FROM clause. The xSelectCallback2()
162 ** method is invoked following the walk of the expressions and FROM clause,
163 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
164 ** and if the expressions and FROM clause both return WRC_Continue;
166 ** Return WRC_Continue under normal conditions. Return WRC_Abort if
167 ** there is an abort request.
169 ** If the Walker does not have an xSelectCallback() then this routine
170 ** is a no-op returning WRC_Continue.
172 int sqlite3WalkSelect(Walker
*pWalker
, Select
*p
){
174 if( p
==0 ) return WRC_Continue
;
175 if( pWalker
->xSelectCallback
==0 ) return WRC_Continue
;
177 rc
= pWalker
->xSelectCallback(pWalker
, p
);
178 if( rc
) return rc
& WRC_Abort
;
179 if( sqlite3WalkSelectExpr(pWalker
, p
)
180 || sqlite3WalkSelectFrom(pWalker
, p
)
184 if( pWalker
->xSelectCallback2
){
185 pWalker
->xSelectCallback2(pWalker
, p
);