github.com/vedadiyan/sqlparser@v1.0.0/pkg/sqlparser/rewriter_api.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sqlparser 18 19 // The rewriter was heavily inspired by https://github.com/golang/tools/blob/master/go/ast/astutil/rewrite.go 20 21 // Rewrite traverses a syntax tree recursively, starting with root, 22 // and calling pre and post for each node as described below. 23 // Rewrite returns the syntax tree, possibly modified. 24 // 25 // If pre is not nil, it is called for each node before the node's 26 // children are traversed (pre-order). If pre returns false, no 27 // children are traversed, and post is not called for that node. 28 // 29 // If post is not nil, and a prior call of pre didn't return false, 30 // post is called for each node after its children are traversed 31 // (post-order). If post returns false, traversal is terminated and 32 // Apply returns immediately. 33 // 34 // Only fields that refer to AST nodes are considered children; 35 // i.e., fields of basic types (strings, []byte, etc.) are ignored. 36 func Rewrite(node SQLNode, pre, post ApplyFunc) (result SQLNode) { 37 parent := &RootNode{node} 38 39 // this is the root-replacer, used when the user replaces the root of the ast 40 replacer := func(newNode SQLNode, _ SQLNode) { 41 parent.SQLNode = newNode 42 } 43 44 a := &application{ 45 pre: pre, 46 post: post, 47 } 48 49 a.rewriteSQLNode(parent, node, replacer) 50 51 return parent.SQLNode 52 } 53 54 // SafeRewrite does not allow replacing nodes on the down walk of the tree walking 55 // Long term this is the only Rewrite functionality we want 56 func SafeRewrite( 57 node SQLNode, 58 shouldVisitChildren func(node SQLNode, parent SQLNode) bool, 59 up ApplyFunc, 60 ) SQLNode { 61 var pre func(cursor *Cursor) bool 62 if shouldVisitChildren != nil { 63 pre = func(cursor *Cursor) bool { 64 visitChildren := shouldVisitChildren(cursor.Node(), cursor.Parent()) 65 if !visitChildren && up != nil { 66 // this gives the up-function a chance to do work on this node even if we are not visiting the children 67 // unfortunately, if the `up` function also returns false for this node, we won't abort the rest of the 68 // tree walking. This is a temporary limitation, and will be fixed when we generated the correct code 69 up(cursor) 70 } 71 return visitChildren 72 } 73 } 74 return Rewrite(node, pre, up) 75 } 76 77 // RootNode is the root node of the AST when rewriting. It is the first element of the tree. 78 type RootNode struct { 79 SQLNode 80 } 81 82 // An ApplyFunc is invoked by Rewrite for each node n, even if n is nil, 83 // before and/or after the node's children, using a Cursor describing 84 // the current node and providing operations on it. 85 // 86 // The return value of ApplyFunc controls the syntax tree traversal. 87 // See Rewrite for details. 88 type ApplyFunc func(*Cursor) bool 89 90 // A Cursor describes a node encountered during Apply. 91 // Information about the node and its parent is available 92 // from the Node and Parent methods. 93 type Cursor struct { 94 parent SQLNode 95 replacer replacerFunc 96 node SQLNode 97 98 // marks that the node has been replaced, and the new node should be visited 99 revisit bool 100 } 101 102 // Node returns the current Node. 103 func (c *Cursor) Node() SQLNode { return c.node } 104 105 // Parent returns the parent of the current Node. 106 func (c *Cursor) Parent() SQLNode { return c.parent } 107 108 // Replace replaces the current node in the parent field with this new object. The use needs to make sure to not 109 // replace the object with something of the wrong type, or the visitor will panic. 110 func (c *Cursor) Replace(newNode SQLNode) { 111 c.replacer(newNode, c.parent) 112 c.node = newNode 113 } 114 115 // ReplacerF returns a replace func that will work even when the cursor has moved to a different node. 116 func (c *Cursor) ReplacerF() func(newNode SQLNode) { 117 replacer := c.replacer 118 parent := c.parent 119 return func(newNode SQLNode) { 120 replacer(newNode, parent) 121 } 122 } 123 124 // ReplaceAndRevisit replaces the current node in the parent field with this new object. 125 // When used, this will abort the visitation of the current node - no post or children visited, 126 // and the new node visited. 127 func (c *Cursor) ReplaceAndRevisit(newNode SQLNode) { 128 switch newNode.(type) { 129 case SelectExprs: 130 default: 131 // We need to add support to the generated code for when to look at the revisit flag. At the moment it is only 132 // there for slices of SQLNode implementations 133 panic("no support added for this type yet") 134 } 135 136 c.replacer(newNode, c.parent) 137 c.node = newNode 138 c.revisit = true 139 } 140 141 type replacerFunc func(newNode, parent SQLNode) 142 143 // application carries all the shared data so we can pass it around cheaply. 144 type application struct { 145 pre, post ApplyFunc 146 cur Cursor 147 }