os/persistentdata/persistentstorage/sql/SQLite364/walker.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sql/SQLite364/walker.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,134 @@
     1.4 +/*
     1.5 +** 2008 August 16
     1.6 +**
     1.7 +** The author disclaims copyright to this source code.  In place of
     1.8 +** a legal notice, here is a blessing:
     1.9 +**
    1.10 +**    May you do good and not evil.
    1.11 +**    May you find forgiveness for yourself and forgive others.
    1.12 +**    May you share freely, never taking more than you give.
    1.13 +**
    1.14 +*************************************************************************
    1.15 +** This file contains routines used for walking the parser tree for
    1.16 +** an SQL statement.
    1.17 +**
    1.18 +** $Id: walker.c,v 1.1 2008/08/20 16:35:10 drh Exp $
    1.19 +*/
    1.20 +#include "sqliteInt.h"
    1.21 +#include <stdlib.h>
    1.22 +#include <string.h>
    1.23 +
    1.24 +
    1.25 +/*
    1.26 +** Walk an expression tree.  Invoke the callback once for each node
    1.27 +** of the expression, while decending.  (In other words, the callback
    1.28 +** is invoked before visiting children.)
    1.29 +**
    1.30 +** The return value from the callback should be one of the WRC_*
    1.31 +** constants to specify how to proceed with the walk.
    1.32 +**
    1.33 +**    WRC_Continue      Continue descending down the tree.
    1.34 +**
    1.35 +**    WRC_Prune         Do not descend into child nodes.  But allow
    1.36 +**                      the walk to continue with sibling nodes.
    1.37 +**
    1.38 +**    WRC_Abort         Do no more callbacks.  Unwind the stack and
    1.39 +**                      return the top-level walk call.
    1.40 +**
    1.41 +** The return value from this routine is WRC_Abort to abandon the tree walk
    1.42 +** and WRC_Continue to continue.
    1.43 +*/
    1.44 +int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
    1.45 +  int rc;
    1.46 +  if( pExpr==0 ) return WRC_Continue;
    1.47 +  rc = pWalker->xExprCallback(pWalker, pExpr);
    1.48 +  if( rc==WRC_Continue ){
    1.49 +    if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
    1.50 +    if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
    1.51 +    if( sqlite3WalkExprList(pWalker, pExpr->pList) ) return WRC_Abort;
    1.52 +    if( sqlite3WalkSelect(pWalker, pExpr->pSelect) ){
    1.53 +      return WRC_Abort;
    1.54 +    }
    1.55 +  }
    1.56 +  return rc & WRC_Abort;
    1.57 +}
    1.58 +
    1.59 +/*
    1.60 +** Call sqlite3WalkExpr() for every expression in list p or until
    1.61 +** an abort request is seen.
    1.62 +*/
    1.63 +int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
    1.64 +  int i, rc = WRC_Continue;
    1.65 +  struct ExprList_item *pItem;
    1.66 +  if( p ){
    1.67 +    for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
    1.68 +      if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
    1.69 +    }
    1.70 +  }
    1.71 +  return rc & WRC_Continue;
    1.72 +}
    1.73 +
    1.74 +/*
    1.75 +** Walk all expressions associated with SELECT statement p.  Do
    1.76 +** not invoke the SELECT callback on p, but do (of course) invoke
    1.77 +** any expr callbacks and SELECT callbacks that come from subqueries.
    1.78 +** Return WRC_Abort or WRC_Continue.
    1.79 +*/
    1.80 +int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
    1.81 +  if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
    1.82 +  if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
    1.83 +  if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
    1.84 +  if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
    1.85 +  if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
    1.86 +  if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
    1.87 +  if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
    1.88 +  return WRC_Continue;
    1.89 +}
    1.90 +
    1.91 +/*
    1.92 +** Walk the parse trees associated with all subqueries in the
    1.93 +** FROM clause of SELECT statement p.  Do not invoke the select
    1.94 +** callback on p, but do invoke it on each FROM clause subquery
    1.95 +** and on any subqueries further down in the tree.  Return 
    1.96 +** WRC_Abort or WRC_Continue;
    1.97 +*/
    1.98 +int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
    1.99 +  SrcList *pSrc;
   1.100 +  int i;
   1.101 +  struct SrcList_item *pItem;
   1.102 +
   1.103 +  pSrc = p->pSrc;
   1.104 +  if( pSrc ){
   1.105 +    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
   1.106 +      if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
   1.107 +        return WRC_Abort;
   1.108 +      }
   1.109 +    }
   1.110 +  }
   1.111 +  return WRC_Continue;
   1.112 +} 
   1.113 +
   1.114 +/*
   1.115 +** Call sqlite3WalkExpr() for every expression in Select statement p.
   1.116 +** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
   1.117 +** on the compound select chain, p->pPrior.
   1.118 +**
   1.119 +** Return WRC_Continue under normal conditions.  Return WRC_Abort if
   1.120 +** there is an abort request.
   1.121 +**
   1.122 +** If the Walker does not have an xSelectCallback() then this routine
   1.123 +** is a no-op returning WRC_Continue.
   1.124 +*/
   1.125 +int sqlite3WalkSelect(Walker *pWalker, Select *p){
   1.126 +  int rc;
   1.127 +  if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
   1.128 +  rc = WRC_Continue;
   1.129 +  while( p  ){
   1.130 +    rc = pWalker->xSelectCallback(pWalker, p);
   1.131 +    if( rc ) break;
   1.132 +    if( sqlite3WalkSelectExpr(pWalker, p) ) return WRC_Abort;
   1.133 +    if( sqlite3WalkSelectFrom(pWalker, p) ) return WRC_Abort;
   1.134 +    p = p->pPrior;
   1.135 +  }
   1.136 +  return rc & WRC_Abort;
   1.137 +}