blob: c31d94f0bf4d52ee05542381d30e95bf4ca73105 [file] [log] [blame]
drh7d10d5a2008-08-20 16:35:10 +00001/*
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.
drh7d10d5a2008-08-20 16:35:10 +000014*/
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.reid60ec9142014-09-06 16:39:46 +000022** of the expression, while descending. (In other words, the callback
drh7d10d5a2008-08-20 16:35:10 +000023** 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**
drh567bd442017-03-07 12:18:23 +000030** WRC_Prune Do not descend into child nodes, but allow
drh7d10d5a2008-08-20 16:35:10 +000031** the walk to continue with sibling nodes.
32**
33** WRC_Abort Do no more callbacks. Unwind the stack and
drh567bd442017-03-07 12:18:23 +000034** return from the top-level walk call.
drh7d10d5a2008-08-20 16:35:10 +000035**
36** The return value from this routine is WRC_Abort to abandon the tree walk
37** and WRC_Continue to continue.
38*/
drh50922cf2016-01-11 14:19:14 +000039static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +000040 int rc;
drh0a9aa222009-04-08 12:21:30 +000041 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
drh0a9aa222009-04-08 12:21:30 +000042 testcase( ExprHasProperty(pExpr, EP_Reduced) );
drhdceed862017-08-17 19:23:16 +000043 while(1){
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 );
49 if( pExpr->pRight ){
50 pExpr = pExpr->pRight;
51 continue;
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;
56 }
dan867be212018-06-25 11:42:08 +000057#ifndef SQLITE_OMIT_WINDOWFUNC
drheda079c2018-09-20 19:02:15 +000058 if( ExprHasProperty(pExpr, EP_WinFunc) ){
59 Window *pWin = pExpr->y.pWin;
danc3163072018-06-23 19:29:56 +000060 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;
63 }
dan867be212018-06-25 11:42:08 +000064#endif
drhc2d14a92017-07-07 12:58:30 +000065 }
drhdceed862017-08-17 19:23:16 +000066 break;
drh7d10d5a2008-08-20 16:35:10 +000067 }
drh922802c2016-08-10 18:56:32 +000068 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +000069}
drh50922cf2016-01-11 14:19:14 +000070int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
71 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
72}
drh7d10d5a2008-08-20 16:35:10 +000073
74/*
75** Call sqlite3WalkExpr() for every expression in list p or until
76** an abort request is seen.
77*/
78int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
drhf7828b52009-06-15 23:15:59 +000079 int i;
drh7d10d5a2008-08-20 16:35:10 +000080 struct ExprList_item *pItem;
81 if( p ){
82 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
83 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
84 }
85 }
drhf7828b52009-06-15 23:15:59 +000086 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +000087}
88
89/*
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.
94*/
95int 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;
drh7d10d5a2008-08-20 16:35:10 +0000102 return WRC_Continue;
103}
104
105/*
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;
111*/
112int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
113 SrcList *pSrc;
114 int i;
115 struct SrcList_item *pItem;
116
117 pSrc = p->pSrc;
drh9d9c41e2017-10-31 03:40:15 +0000118 assert( pSrc!=0 );
119 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
120 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
121 return WRC_Abort;
122 }
123 if( pItem->fg.isTabFunc
124 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
125 ){
126 return WRC_Abort;
drh7d10d5a2008-08-20 16:35:10 +0000127 }
128 }
129 return WRC_Continue;
130}
131
132/*
133** Call sqlite3WalkExpr() for every expression in Select statement p.
134** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
danb290f112014-01-17 14:59:27 +0000135** on the compound select chain, p->pPrior.
136**
137** If it is not NULL, the xSelectCallback() callback is invoked before
138** the walk of the expressions and FROM clause. The xSelectCallback2()
drh979dd1b2017-05-29 14:26:07 +0000139** 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;
drh7d10d5a2008-08-20 16:35:10 +0000142**
143** Return WRC_Continue under normal conditions. Return WRC_Abort if
144** there is an abort request.
145**
146** If the Walker does not have an xSelectCallback() then this routine
147** is a no-op returning WRC_Continue.
148*/
149int sqlite3WalkSelect(Walker *pWalker, Select *p){
150 int rc;
drh3da70a62017-07-07 11:49:48 +0000151 if( p==0 ) return WRC_Continue;
152 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
drh979dd1b2017-05-29 14:26:07 +0000153 do{
154 rc = pWalker->xSelectCallback(pWalker, p);
155 if( rc ) return rc & WRC_Abort;
drhed551b92012-08-23 19:46:11 +0000156 if( sqlite3WalkSelectExpr(pWalker, p)
157 || sqlite3WalkSelectFrom(pWalker, p)
158 ){
drhed551b92012-08-23 19:46:11 +0000159 return WRC_Abort;
160 }
danb290f112014-01-17 14:59:27 +0000161 if( pWalker->xSelectCallback2 ){
162 pWalker->xSelectCallback2(pWalker, p);
drhaa87f9a2013-04-25 00:57:10 +0000163 }
drh7d10d5a2008-08-20 16:35:10 +0000164 p = p->pPrior;
drh979dd1b2017-05-29 14:26:07 +0000165 }while( p!=0 );
166 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +0000167}