github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/ast/stringer.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package ast
    15  
    16  import (
    17  	"fmt"
    18  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    19  )
    20  
    21  // ToString converts a node to a string for debugging purpose.
    22  func ToString(node Node) string {
    23  	s := &stringer{strMap: map[Node]string{}}
    24  	node.Accept(s)
    25  	return s.strMap[node]
    26  }
    27  
    28  type stringer struct {
    29  	strMap map[Node]string
    30  }
    31  
    32  // Enter implements Visitor Enter interface.
    33  func (c *stringer) Enter(node Node) (Node, bool) {
    34  	return node, false
    35  }
    36  
    37  // Leave implements Visitor Leave interface.
    38  func (c *stringer) Leave(in Node) (out Node, ok bool) {
    39  	switch x := in.(type) {
    40  	case *BinaryOperationExpr:
    41  		left := c.strMap[x.L]
    42  		right := c.strMap[x.R]
    43  		c.strMap[x] = left + " " + x.Op.String() + " " + right
    44  	case *ValueExpr:
    45  		str, _ := types.ToString(x.GetValue())
    46  		c.strMap[x] = str
    47  	case *ParenthesesExpr:
    48  		c.strMap[x] = "(" + c.strMap[x.Expr] + ")"
    49  	case *ColumnNameExpr:
    50  		c.strMap[x] = x.Name.Table.O + "." + x.Name.Name.O
    51  	case *BetweenExpr:
    52  		c.strMap[x] = c.strMap[x.Expr] + " BETWWEN " + c.strMap[x.Left] + " AND " + c.strMap[x.Right]
    53  	default:
    54  		c.strMap[in] = fmt.Sprintf("%T", in)
    55  	}
    56  	return in, true
    57  }