Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
walker.c262 linesDownload Raw Back to src
1/*2** 2008 August 163**4** The author disclaims copyright to this source code.  In place of5** 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 for13** an SQL statement.14*/15#include "sqliteInt.h"16#include <stdlib.h>17#include <string.h>18 19 20#if !defined(SQLITE_OMIT_WINDOWFUNC)21/*22** Walk all expressions linked into the list of Window objects passed23** as the second argument.24*/25static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){26  Window *pWin;27  for(pWin=pList; pWin; pWin=pWin->pNextWin){28    int rc;29    rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);30    if( rc ) return WRC_Abort;31    rc = sqlite3WalkExprList(pWalker, pWin->pPartition);32    if( rc ) return WRC_Abort;33    rc = sqlite3WalkExpr(pWalker, pWin->pFilter);34    if( rc ) return WRC_Abort;35    rc = sqlite3WalkExpr(pWalker, pWin->pStart);36    if( rc ) return WRC_Abort;37    rc = sqlite3WalkExpr(pWalker, pWin->pEnd);38    if( rc ) return WRC_Abort;39    if( bOneOnly ) break;40  }41  return WRC_Continue;42}43#endif44 45/*46** Walk an expression tree.  Invoke the callback once for each node47** of the expression, while descending.  (In other words, the callback48** is invoked before visiting children.)49**50** The return value from the callback should be one of the WRC_*51** constants to specify how to proceed with the walk.52**53**    WRC_Continue      Continue descending down the tree.54**55**    WRC_Prune         Do not descend into child nodes, but allow56**                      the walk to continue with sibling nodes.57**58**    WRC_Abort         Do no more callbacks.  Unwind the stack and59**                      return from the top-level walk call.60**61** The return value from this routine is WRC_Abort to abandon the tree walk62** and WRC_Continue to continue.63*/64SQLITE_NOINLINE int sqlite3WalkExprNN(Walker *pWalker, Expr *pExpr){65  int rc;66  testcase( ExprHasProperty(pExpr, EP_TokenOnly) );67  testcase( ExprHasProperty(pExpr, EP_Reduced) );68  while(1){69    rc = pWalker->xExprCallback(pWalker, pExpr);70    if( rc ) return rc & WRC_Abort;71    if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){72      assert( pExpr->x.pList==0 || pExpr->pRight==0 );73      if( pExpr->pLeft && sqlite3WalkExprNN(pWalker, pExpr->pLeft) ){74        return WRC_Abort;75      }76      if( pExpr->pRight ){77        assert( !ExprHasProperty(pExpr, EP_WinFunc) );78        pExpr = pExpr->pRight;79        continue;80      }else if( ExprUseXSelect(pExpr) ){81        assert( !ExprHasProperty(pExpr, EP_WinFunc) );82        if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;83      }else{84        if( pExpr->x.pList ){85          if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;86        }87#ifndef SQLITE_OMIT_WINDOWFUNC88        if( ExprHasProperty(pExpr, EP_WinFunc) ){89          if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;90        }91#endif92      }93    }94    break;95  }96  return WRC_Continue;97}98int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){99  return pExpr ? sqlite3WalkExprNN(pWalker,pExpr) : WRC_Continue;100}101 102/*103** Call sqlite3WalkExpr() for every expression in list p or until104** an abort request is seen.105*/106int sqlite3WalkExprList(Walker *pWalker, ExprList *p){107  int i;108  struct ExprList_item *pItem;109  if( p ){110    for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){111      if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;112    }113  }114  return WRC_Continue;115}116 117/*118** This is a no-op callback for Walker->xSelectCallback2.  If this119** callback is set, then the Select->pWinDefn list is traversed.120*/121void sqlite3WalkWinDefnDummyCallback(Walker *pWalker, Select *p){122  UNUSED_PARAMETER(pWalker);123  UNUSED_PARAMETER(p);124  /* No-op */125}126 127/*128** Walk all expressions associated with SELECT statement p.  Do129** not invoke the SELECT callback on p, but do (of course) invoke130** any expr callbacks and SELECT callbacks that come from subqueries.131** Return WRC_Abort or WRC_Continue.132*/133int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){134  if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;135  if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;136  if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;137  if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;138  if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;139  if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;140#if !defined(SQLITE_OMIT_WINDOWFUNC)141  if( p->pWinDefn ){142    Parse *pParse;143    if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback144     || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)145#ifndef SQLITE_OMIT_CTE146     || pWalker->xSelectCallback2==sqlite3SelectPopWith147#endif148    ){149      /* The following may return WRC_Abort if there are unresolvable150      ** symbols (e.g. a table that does not exist) in a window definition. */151      int rc = walkWindowList(pWalker, p->pWinDefn, 0);152      return rc;153    }154  }155#endif156  return WRC_Continue;157}158 159/*160** Walk the parse trees associated with all subqueries in the161** FROM clause of SELECT statement p.  Do not invoke the select162** callback on p, but do invoke it on each FROM clause subquery163** and on any subqueries further down in the tree.  Return 164** WRC_Abort or WRC_Continue;165*/166int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){167  SrcList *pSrc;168  int i;169  SrcItem *pItem;170 171  pSrc = p->pSrc;172  if( ALWAYS(pSrc) ){173    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){174      if( pItem->fg.isSubquery175       && sqlite3WalkSelect(pWalker, pItem->u4.pSubq->pSelect)176      ){177        return WRC_Abort;178      }179      if( pItem->fg.isTabFunc180       && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)181      ){182        return WRC_Abort;183      }184    }185  }186  return WRC_Continue;187}188 189/*190** Call sqlite3WalkExpr() for every expression in Select statement p.191** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and192** on the compound select chain, p->pPrior. 193**194** If it is not NULL, the xSelectCallback() callback is invoked before195** the walk of the expressions and FROM clause. The xSelectCallback2()196** method is invoked following the walk of the expressions and FROM clause,197** but only if both xSelectCallback and xSelectCallback2 are both non-NULL198** and if the expressions and FROM clause both return WRC_Continue;199**200** Return WRC_Continue under normal conditions.  Return WRC_Abort if201** there is an abort request.202**203** If the Walker does not have an xSelectCallback() then this routine204** is a no-op returning WRC_Continue.205*/206int sqlite3WalkSelect(Walker *pWalker, Select *p){207  int rc;208  if( p==0 ) return WRC_Continue;209  if( pWalker->xSelectCallback==0 ) return WRC_Continue;210  do{211    rc = pWalker->xSelectCallback(pWalker, p);212    if( rc ) return rc & WRC_Abort;213    if( sqlite3WalkSelectExpr(pWalker, p)214     || sqlite3WalkSelectFrom(pWalker, p)215    ){216      return WRC_Abort;217    }218    if( pWalker->xSelectCallback2 ){219      pWalker->xSelectCallback2(pWalker, p);220    }221    p = p->pPrior;222  }while( p!=0 );223  return WRC_Continue;224}225 226/* Increase the walkerDepth when entering a subquery, and227** decrease when leaving the subquery.228*/229int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){230  UNUSED_PARAMETER(pSelect);231  pWalker->walkerDepth++;232  return WRC_Continue;233}234void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){235  UNUSED_PARAMETER(pSelect);236  pWalker->walkerDepth--;237}238 239 240/*241** No-op routine for the parse-tree walker.242**243** When this routine is the Walker.xExprCallback then expression trees244** are walked without any actions being taken at each node.  Presumably,245** when this routine is used for Walker.xExprCallback then 246** Walker.xSelectCallback is set to do something useful for every 247** subquery in the parser tree.248*/249int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){250  UNUSED_PARAMETER2(NotUsed, NotUsed2);251  return WRC_Continue;252}253 254/*255** No-op routine for the parse-tree walker for SELECT statements.256** subquery in the parser tree.257*/258int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){259  UNUSED_PARAMETER2(NotUsed, NotUsed2);260  return WRC_Continue;261}262