walker.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  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.
  14. */
  15. #include "sqliteInt.h"
  16. #include <stdlib.h>
  17. #include <string.h>
  18. /*
  19. ** Walk an expression tree. Invoke the callback once for each node
  20. ** of the expression, while decending. (In other words, the callback
  21. ** is invoked before visiting children.)
  22. **
  23. ** The return value from the callback should be one of the WRC_*
  24. ** constants to specify how to proceed with the walk.
  25. **
  26. ** WRC_Continue Continue descending down the tree.
  27. **
  28. ** WRC_Prune Do not descend into child nodes. But allow
  29. ** the walk to continue with sibling nodes.
  30. **
  31. ** WRC_Abort Do no more callbacks. Unwind the stack and
  32. ** return the top-level walk call.
  33. **
  34. ** The return value from this routine is WRC_Abort to abandon the tree walk
  35. ** and WRC_Continue to continue.
  36. */
  37. int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
  38. int rc;
  39. if( pExpr==0 ) return WRC_Continue;
  40. testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
  41. testcase( ExprHasProperty(pExpr, EP_Reduced) );
  42. rc = pWalker->xExprCallback(pWalker, pExpr);
  43. if( rc==WRC_Continue
  44. && !ExprHasProperty(pExpr,EP_TokenOnly) ){
  45. if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
  46. if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
  47. if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  48. if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
  49. }else{
  50. if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
  51. }
  52. }
  53. return rc & WRC_Abort;
  54. }
  55. /*
  56. ** Call sqlite3WalkExpr() for every expression in list p or until
  57. ** an abort request is seen.
  58. */
  59. int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
  60. int i;
  61. struct ExprList_item *pItem;
  62. if( p ){
  63. for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
  64. if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
  65. }
  66. }
  67. return WRC_Continue;
  68. }
  69. /*
  70. ** Walk all expressions associated with SELECT statement p. Do
  71. ** not invoke the SELECT callback on p, but do (of course) invoke
  72. ** any expr callbacks and SELECT callbacks that come from subqueries.
  73. ** Return WRC_Abort or WRC_Continue.
  74. */
  75. int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
  76. if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
  77. if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
  78. if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
  79. if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
  80. if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
  81. if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
  82. if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
  83. return WRC_Continue;
  84. }
  85. /*
  86. ** Walk the parse trees associated with all subqueries in the
  87. ** FROM clause of SELECT statement p. Do not invoke the select
  88. ** callback on p, but do invoke it on each FROM clause subquery
  89. ** and on any subqueries further down in the tree. Return
  90. ** WRC_Abort or WRC_Continue;
  91. */
  92. int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
  93. SrcList *pSrc;
  94. int i;
  95. struct SrcList_item *pItem;
  96. pSrc = p->pSrc;
  97. if( ALWAYS(pSrc) ){
  98. for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
  99. if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
  100. return WRC_Abort;
  101. }
  102. }
  103. }
  104. return WRC_Continue;
  105. }
  106. /*
  107. ** Call sqlite3WalkExpr() for every expression in Select statement p.
  108. ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
  109. ** on the compound select chain, p->pPrior. Invoke the xSelectCallback()
  110. ** either before or after the walk of expressions and FROM clause, depending
  111. ** on whether pWalker->bSelectDepthFirst is false or true, respectively.
  112. **
  113. ** Return WRC_Continue under normal conditions. Return WRC_Abort if
  114. ** there is an abort request.
  115. **
  116. ** If the Walker does not have an xSelectCallback() then this routine
  117. ** is a no-op returning WRC_Continue.
  118. */
  119. int sqlite3WalkSelect(Walker *pWalker, Select *p){
  120. int rc;
  121. if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
  122. rc = WRC_Continue;
  123. pWalker->walkerDepth++;
  124. while( p ){
  125. if( !pWalker->bSelectDepthFirst ){
  126. rc = pWalker->xSelectCallback(pWalker, p);
  127. if( rc ) break;
  128. }
  129. if( sqlite3WalkSelectExpr(pWalker, p)
  130. || sqlite3WalkSelectFrom(pWalker, p)
  131. ){
  132. pWalker->walkerDepth--;
  133. return WRC_Abort;
  134. }
  135. if( pWalker->bSelectDepthFirst ){
  136. rc = pWalker->xSelectCallback(pWalker, p);
  137. /* Depth-first search is currently only used for
  138. ** selectAddSubqueryTypeInfo() and that routine always returns
  139. ** WRC_Continue (0). So the following branch is never taken. */
  140. if( NEVER(rc) ) break;
  141. }
  142. p = p->pPrior;
  143. }
  144. pWalker->walkerDepth--;
  145. return rc & WRC_Abort;
  146. }