github.com/pingcap/tidb/parser@v0.0.0-20231013125129-93a834a6bf8d/ast/base.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  	"sync"
    18  
    19  	"github.com/pingcap/tidb/parser/charset"
    20  	"github.com/pingcap/tidb/parser/types"
    21  )
    22  
    23  // node is the struct implements Node interface except for Accept method.
    24  // Node implementations should embed it in.
    25  type node struct {
    26  	utf8Text string
    27  	enc      charset.Encoding
    28  	once     *sync.Once
    29  
    30  	text   string
    31  	offset int
    32  }
    33  
    34  // SetOriginTextPosition implements Node interface.
    35  func (n *node) SetOriginTextPosition(offset int) {
    36  	n.offset = offset
    37  }
    38  
    39  // OriginTextPosition implements Node interface.
    40  func (n *node) OriginTextPosition() int {
    41  	return n.offset
    42  }
    43  
    44  // SetText implements Node interface.
    45  func (n *node) SetText(enc charset.Encoding, text string) {
    46  	n.enc = enc
    47  	n.text = text
    48  	n.once = &sync.Once{}
    49  }
    50  
    51  // Text implements Node interface.
    52  func (n *node) Text() string {
    53  	if n.once == nil {
    54  		return n.text
    55  	}
    56  	n.once.Do(func() {
    57  		if n.enc == nil {
    58  			n.utf8Text = n.text
    59  			return
    60  		}
    61  		utf8Lit, _ := n.enc.Transform(nil, charset.HackSlice(n.text), charset.OpDecodeReplace)
    62  		n.utf8Text = charset.HackString(utf8Lit)
    63  	})
    64  	return n.utf8Text
    65  }
    66  
    67  // OriginalText implements Node interface.
    68  func (n *node) OriginalText() string {
    69  	return n.text
    70  }
    71  
    72  // stmtNode implements StmtNode interface.
    73  // Statement implementations should embed it in.
    74  type stmtNode struct {
    75  	node
    76  }
    77  
    78  // statement implements StmtNode interface.
    79  func (sn *stmtNode) statement() {}
    80  
    81  // ddlNode implements DDLNode interface.
    82  // DDL implementations should embed it in.
    83  type ddlNode struct {
    84  	stmtNode
    85  }
    86  
    87  // ddlStatement implements DDLNode interface.
    88  func (dn *ddlNode) ddlStatement() {}
    89  
    90  // dmlNode is the struct implements DMLNode interface.
    91  // DML implementations should embed it in.
    92  type dmlNode struct {
    93  	stmtNode
    94  }
    95  
    96  // dmlStatement implements DMLNode interface.
    97  func (dn *dmlNode) dmlStatement() {}
    98  
    99  // exprNode is the struct implements Expression interface.
   100  // Expression implementations should embed it in.
   101  type exprNode struct {
   102  	node
   103  	Type types.FieldType
   104  	flag uint64
   105  }
   106  
   107  // TexprNode is exported for parser driver.
   108  type TexprNode = exprNode
   109  
   110  // SetType implements ExprNode interface.
   111  func (en *exprNode) SetType(tp *types.FieldType) {
   112  	en.Type = *tp
   113  }
   114  
   115  // GetType implements ExprNode interface.
   116  func (en *exprNode) GetType() *types.FieldType {
   117  	return &en.Type
   118  }
   119  
   120  // SetFlag implements ExprNode interface.
   121  func (en *exprNode) SetFlag(flag uint64) {
   122  	en.flag = flag
   123  }
   124  
   125  // GetFlag implements ExprNode interface.
   126  func (en *exprNode) GetFlag() uint64 {
   127  	return en.flag
   128  }
   129  
   130  type funcNode struct {
   131  	exprNode
   132  }
   133  
   134  // functionExpression implements FunctionNode interface.
   135  func (fn *funcNode) functionExpression() {}