blob: 81e0f2cd60b85ea72bbaa4c877a9cbd9029d0503 [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*/
39int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
40 int rc;
41 if( pExpr==0 ) return WRC_Continue;
drh0a9aa222009-04-08 12:21:30 +000042 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
drh0a9aa222009-04-08 12:21:30 +000043 testcase( ExprHasProperty(pExpr, EP_Reduced) );
drh7d10d5a2008-08-20 16:35:10 +000044 rc = pWalker->xExprCallback(pWalker, pExpr);
drh12ffee82009-04-08 13:51:51 +000045 if( rc==WRC_Continue
drhc5cd1242013-09-12 16:50:49 +000046 && !ExprHasProperty(pExpr,EP_TokenOnly) ){
drh7d10d5a2008-08-20 16:35:10 +000047 if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
48 if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
danielk19776ab3a2e2009-02-19 14:39:25 +000049 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
50 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
51 }else{
52 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
drh7d10d5a2008-08-20 16:35:10 +000053 }
54 }
55 return rc & WRC_Abort;
56}
57
58/*
59** Call sqlite3WalkExpr() for every expression in list p or until
60** an abort request is seen.
61*/
62int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
drhf7828b52009-06-15 23:15:59 +000063 int i;
drh7d10d5a2008-08-20 16:35:10 +000064 struct ExprList_item *pItem;
65 if( p ){
66 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
67 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
68 }
69 }
drhf7828b52009-06-15 23:15:59 +000070 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +000071}
72
73/*
74** Walk all expressions associated with SELECT statement p. Do
75** not invoke the SELECT callback on p, but do (of course) invoke
76** any expr callbacks and SELECT callbacks that come from subqueries.
77** Return WRC_Abort or WRC_Continue.
78*/
79int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
80 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
81 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
82 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
83 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
84 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
85 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
86 if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
87 return WRC_Continue;
88}
89
90/*
91** Walk the parse trees associated with all subqueries in the
92** FROM clause of SELECT statement p. Do not invoke the select
93** callback on p, but do invoke it on each FROM clause subquery
94** and on any subqueries further down in the tree. Return
95** WRC_Abort or WRC_Continue;
96*/
97int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
98 SrcList *pSrc;
99 int i;
100 struct SrcList_item *pItem;
101
102 pSrc = p->pSrc;
drhf7828b52009-06-15 23:15:59 +0000103 if( ALWAYS(pSrc) ){
drh7d10d5a2008-08-20 16:35:10 +0000104 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
105 if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
106 return WRC_Abort;
107 }
drh01d230c2015-08-19 17:11:37 +0000108 if( pItem->fg.isTabFunc
109 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
110 ){
111 return WRC_Abort;
112 }
drh7d10d5a2008-08-20 16:35:10 +0000113 }
114 }
115 return WRC_Continue;
116}
117
118/*
119** Call sqlite3WalkExpr() for every expression in Select statement p.
120** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
danb290f112014-01-17 14:59:27 +0000121** on the compound select chain, p->pPrior.
122**
123** If it is not NULL, the xSelectCallback() callback is invoked before
124** the walk of the expressions and FROM clause. The xSelectCallback2()
125** method, if it is not NULL, is invoked following the walk of the
126** expressions and FROM clause.
drh7d10d5a2008-08-20 16:35:10 +0000127**
128** Return WRC_Continue under normal conditions. Return WRC_Abort if
129** there is an abort request.
130**
131** If the Walker does not have an xSelectCallback() then this routine
132** is a no-op returning WRC_Continue.
133*/
134int sqlite3WalkSelect(Walker *pWalker, Select *p){
135 int rc;
danb290f112014-01-17 14:59:27 +0000136 if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){
137 return WRC_Continue;
138 }
drh7d10d5a2008-08-20 16:35:10 +0000139 rc = WRC_Continue;
drhed551b92012-08-23 19:46:11 +0000140 pWalker->walkerDepth++;
141 while( p ){
danb290f112014-01-17 14:59:27 +0000142 if( pWalker->xSelectCallback ){
drhaa87f9a2013-04-25 00:57:10 +0000143 rc = pWalker->xSelectCallback(pWalker, p);
144 if( rc ) break;
145 }
drhed551b92012-08-23 19:46:11 +0000146 if( sqlite3WalkSelectExpr(pWalker, p)
147 || sqlite3WalkSelectFrom(pWalker, p)
148 ){
149 pWalker->walkerDepth--;
150 return WRC_Abort;
151 }
danb290f112014-01-17 14:59:27 +0000152 if( pWalker->xSelectCallback2 ){
153 pWalker->xSelectCallback2(pWalker, p);
drhaa87f9a2013-04-25 00:57:10 +0000154 }
drh7d10d5a2008-08-20 16:35:10 +0000155 p = p->pPrior;
156 }
drhed551b92012-08-23 19:46:11 +0000157 pWalker->walkerDepth--;
drh7d10d5a2008-08-20 16:35:10 +0000158 return rc & WRC_Abort;
159}