blob: d1b1e96a2d1eb7908ad32673bf7880cde4bb2fad [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**
30** WRC_Prune Do not descend into child nodes. But allow
31** the walk to continue with sibling nodes.
32**
33** WRC_Abort Do no more callbacks. Unwind the stack and
34** return the top-level walk call.
35**
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) );
drh7d10d5a2008-08-20 16:35:10 +000043 rc = pWalker->xExprCallback(pWalker, pExpr);
drh209bc522016-09-23 21:36:24 +000044 if( rc || ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
45 return rc & WRC_Abort;
46 }
drh922802c2016-08-10 18:56:32 +000047 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
48 if( pExpr->pRight && walkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
49 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
50 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
drhd43075b2016-09-19 02:19:00 +000051 }else if( pExpr->x.pList ){
drh922802c2016-08-10 18:56:32 +000052 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
drh7d10d5a2008-08-20 16:35:10 +000053 }
drh922802c2016-08-10 18:56:32 +000054 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +000055}
drh50922cf2016-01-11 14:19:14 +000056int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
57 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
58}
drh7d10d5a2008-08-20 16:35:10 +000059
60/*
61** Call sqlite3WalkExpr() for every expression in list p or until
62** an abort request is seen.
63*/
64int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
drhf7828b52009-06-15 23:15:59 +000065 int i;
drh7d10d5a2008-08-20 16:35:10 +000066 struct ExprList_item *pItem;
67 if( p ){
68 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
69 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
70 }
71 }
drhf7828b52009-06-15 23:15:59 +000072 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +000073}
74
75/*
76** Walk all expressions associated with SELECT statement p. Do
77** not invoke the SELECT callback on p, but do (of course) invoke
78** any expr callbacks and SELECT callbacks that come from subqueries.
79** Return WRC_Abort or WRC_Continue.
80*/
81int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
82 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
83 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
84 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
85 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
86 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
87 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
88 if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
89 return WRC_Continue;
90}
91
92/*
93** Walk the parse trees associated with all subqueries in the
94** FROM clause of SELECT statement p. Do not invoke the select
95** callback on p, but do invoke it on each FROM clause subquery
96** and on any subqueries further down in the tree. Return
97** WRC_Abort or WRC_Continue;
98*/
99int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
100 SrcList *pSrc;
101 int i;
102 struct SrcList_item *pItem;
103
104 pSrc = p->pSrc;
drhf7828b52009-06-15 23:15:59 +0000105 if( ALWAYS(pSrc) ){
drh7d10d5a2008-08-20 16:35:10 +0000106 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
107 if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
108 return WRC_Abort;
109 }
drh01d230c2015-08-19 17:11:37 +0000110 if( pItem->fg.isTabFunc
111 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
112 ){
113 return WRC_Abort;
114 }
drh7d10d5a2008-08-20 16:35:10 +0000115 }
116 }
117 return WRC_Continue;
118}
119
120/*
121** Call sqlite3WalkExpr() for every expression in Select statement p.
122** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
danb290f112014-01-17 14:59:27 +0000123** on the compound select chain, p->pPrior.
124**
125** If it is not NULL, the xSelectCallback() callback is invoked before
126** the walk of the expressions and FROM clause. The xSelectCallback2()
127** method, if it is not NULL, is invoked following the walk of the
128** expressions and FROM clause.
drh7d10d5a2008-08-20 16:35:10 +0000129**
130** Return WRC_Continue under normal conditions. Return WRC_Abort if
131** there is an abort request.
132**
133** If the Walker does not have an xSelectCallback() then this routine
134** is a no-op returning WRC_Continue.
135*/
136int sqlite3WalkSelect(Walker *pWalker, Select *p){
137 int rc;
danb290f112014-01-17 14:59:27 +0000138 if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){
139 return WRC_Continue;
140 }
drh7d10d5a2008-08-20 16:35:10 +0000141 rc = WRC_Continue;
drhed551b92012-08-23 19:46:11 +0000142 pWalker->walkerDepth++;
143 while( p ){
danb290f112014-01-17 14:59:27 +0000144 if( pWalker->xSelectCallback ){
drhaa87f9a2013-04-25 00:57:10 +0000145 rc = pWalker->xSelectCallback(pWalker, p);
146 if( rc ) break;
147 }
drhed551b92012-08-23 19:46:11 +0000148 if( sqlite3WalkSelectExpr(pWalker, p)
149 || sqlite3WalkSelectFrom(pWalker, p)
150 ){
151 pWalker->walkerDepth--;
152 return WRC_Abort;
153 }
danb290f112014-01-17 14:59:27 +0000154 if( pWalker->xSelectCallback2 ){
155 pWalker->xSelectCallback2(pWalker, p);
drhaa87f9a2013-04-25 00:57:10 +0000156 }
drh7d10d5a2008-08-20 16:35:10 +0000157 p = p->pPrior;
158 }
drhed551b92012-08-23 19:46:11 +0000159 pWalker->walkerDepth--;
drh7d10d5a2008-08-20 16:35:10 +0000160 return rc & WRC_Abort;
161}