github.com/XiaoMi/Gaea@v1.2.5/util/types.go (about)

     1  // Copyright 2019 The Gaea Authors. All Rights Reserved.
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/XiaoMi/Gaea/parser/format"
    21  	types "github.com/XiaoMi/Gaea/parser/tidb-types"
    22  	driver "github.com/XiaoMi/Gaea/parser/tidb-types/parser_driver"
    23  )
    24  
    25  // GetValueExprResult copy from ValueExpr.Restore()
    26  // TODO: 分表列是否需要支持等值比较NULL
    27  func GetValueExprResult(n *driver.ValueExpr) (interface{}, error) {
    28  	switch n.Kind() {
    29  	case types.KindNull:
    30  		return nil, nil // TODO: null str or nil?
    31  	case types.KindInt64:
    32  		return n.GetInt64(), nil
    33  	case types.KindUint64:
    34  		return n.GetUint64(), nil
    35  	case types.KindFloat32:
    36  		return n.GetFloat32(), nil
    37  	case types.KindFloat64:
    38  		return n.GetFloat64(), nil
    39  	case types.KindString, types.KindBytes:
    40  		return n.GetString(), nil
    41  	default:
    42  		s := &strings.Builder{}
    43  		ctx := format.NewRestoreCtx(format.EscapeRestoreFlags, s)
    44  		err := n.Restore(ctx)
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  		return s.String(), nil
    49  	}
    50  }