github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/tree/constant.go (about) 1 // Copyright 2021 Matrix Origin 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 tree 16 17 import ( 18 "go/constant" 19 "strings" 20 ) 21 22 // the AST for literals like string,numeric,bool and etc. 23 type Constant interface { 24 Expr 25 } 26 27 type P_TYPE uint8 28 29 const ( 30 P_any P_TYPE = iota 31 P_hexnum 32 P_null 33 P_bool 34 P_int64 35 P_uint64 36 P_float64 37 P_char 38 P_decimal 39 P_bit 40 P_ScoreBinary 41 P_nulltext 42 ) 43 44 // the AST for the constant numeric value. 45 type NumVal struct { 46 Constant 47 Value constant.Value 48 49 // negative is the sign label 50 negative bool 51 52 // origString is the "original" string literals that should remain sign-less. 53 origString string 54 55 //converted result 56 resInt int64 57 resFloat float64 58 ValType P_TYPE 59 } 60 61 func (n *NumVal) Format(ctx *FmtCtx) { 62 if n.origString != "" { 63 ctx.WriteValue(n.ValType, FormatString(n.origString)) 64 return 65 } 66 switch n.Value.Kind() { 67 case constant.String: 68 ctx.WriteValue(n.ValType, n.origString) 69 case constant.Bool: 70 ctx.WriteString(strings.ToLower(n.Value.String())) 71 case constant.Unknown: 72 ctx.WriteString("null") 73 } 74 } 75 76 func FormatString(str string) string { 77 var buffer strings.Builder 78 for i, ch := range str { 79 if ch == '\n' { 80 buffer.WriteString("\\n") 81 } else if ch == '\x00' { 82 buffer.WriteString("\\0") 83 } else if ch == '\r' { 84 buffer.WriteString("\\r") 85 } else if ch == '\\' { 86 if (i + 1) < len(str) { 87 if str[i+1] == '_' || str[i+1] == '%' { 88 buffer.WriteByte('\\') 89 continue 90 } 91 } 92 buffer.WriteString("\\\\") 93 } else if ch == 8 { 94 buffer.WriteString("\\b") 95 } else if ch == 26 { 96 buffer.WriteString("\\Z") 97 } else if ch == '\t' { 98 buffer.WriteString("\\t") 99 } else { 100 // buffer.WriteByte(byte(ch)) 101 buffer.WriteRune(ch) 102 } 103 } 104 res := buffer.String() 105 return res 106 } 107 108 func (n *NumVal) String() string { 109 return n.origString 110 } 111 112 func (n *NumVal) Negative() bool { 113 return n.negative 114 } 115 116 func NewNumVal(value constant.Value, origString string, negative bool) *NumVal { 117 return &NumVal{ 118 Value: value, 119 origString: origString, 120 negative: negative, 121 } 122 } 123 124 func NewNumValWithType(value constant.Value, origString string, negative bool, typ P_TYPE) *NumVal { 125 numVal := &NumVal{ 126 Value: value, 127 origString: origString, 128 negative: negative, 129 ValType: typ, 130 } 131 return numVal 132 } 133 134 func NewNumValWithResInt(value constant.Value, origString string, negative bool, resInt int64) *NumVal { 135 return &NumVal{ 136 Value: value, 137 origString: origString, 138 negative: negative, 139 resInt: resInt, 140 } 141 } 142 143 func NewNumValWithResFoalt(value constant.Value, origString string, negative bool, resFloat float64) *NumVal { 144 return &NumVal{ 145 Value: value, 146 origString: origString, 147 negative: negative, 148 resFloat: resFloat, 149 } 150 } 151 152 // StrVal represents a constant string value. 153 type StrVal struct { 154 Constant 155 str string 156 } 157 158 func (node *StrVal) Format(ctx *FmtCtx) { 159 ctx.WriteString(node.str) 160 } 161 162 func NewStrVal(s string) *StrVal { 163 return &StrVal{str: s} 164 }