github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgSql/MysqlAst/Node.go (about)

     1  package MysqlAst
     2  
     3  type Node interface {
     4  	GetText() string
     5  	Copy() Node
     6  }
     7  
     8  type PreparedNode interface {
     9  	GetPrepareParameter() (string, []string)
    10  	Copy() PreparedNode
    11  }
    12  
    13  type WhereCondition interface {
    14  	IsEmpty() bool
    15  	AddAndCondition(node PreparedNode) *AndWhereCondition
    16  	PreparedNode
    17  }
    18  
    19  func Text(text string) StringNodeImpl {
    20  	return StringNodeImpl(text)
    21  }
    22  
    23  type StringNodeImpl string
    24  
    25  func (s StringNodeImpl) GetText() string {
    26  	return string(s)
    27  }
    28  
    29  func (s StringNodeImpl) Copy() Node {
    30  	return s
    31  }
    32  
    33  func Prepare(text string, parameterList ...string) StringPrepareNodeImpl {
    34  	return StringPrepareNodeImpl{Text: text, ParameterList: parameterList}
    35  }
    36  
    37  type StringPrepareNodeImpl struct {
    38  	Text          string
    39  	ParameterList []string
    40  }
    41  
    42  func (n StringPrepareNodeImpl) GetPrepareParameter() (string, []string) {
    43  	return n.Text, n.ParameterList
    44  }
    45  func (n StringPrepareNodeImpl) Copy() PreparedNode {
    46  	s := StringPrepareNodeImpl{}
    47  	s.ParameterList = make([]string, len(n.ParameterList))
    48  	copy(s.ParameterList, n.ParameterList)
    49  	s.Text = n.Text
    50  	return s
    51  }
    52  func (n StringPrepareNodeImpl) IsEmpty() bool {
    53  	return n.Text == ""
    54  }
    55  func (n StringPrepareNodeImpl) AddAndCondition(node PreparedNode) *AndWhereCondition {
    56  	return &AndWhereCondition{List: []PreparedNode{n, node}}
    57  }
    58  
    59  func joinPrepareNode(nodeList []PreparedNode, split string, start string, end string) (text string, parameterList []string) {
    60  	text = start
    61  	for i := range nodeList {
    62  		thisText, parameter := nodeList[i].GetPrepareParameter()
    63  		text += thisText
    64  		parameterList = append(parameterList, parameter...)
    65  		if i < len(nodeList)-1 {
    66  			text += split
    67  		}
    68  	}
    69  	text += end
    70  	return
    71  }