github.com/pingcap/tidb/parser@v0.0.0-20231013125129-93a834a6bf8d/types/etc.go (about) 1 // Copyright 2014 The ql Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSES/QL-LICENSE file. 4 5 // Copyright 2015 PingCAP, Inc. 6 // 7 // Licensed under the Apache License, Version 2.0 (the "License"); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package types 19 20 import ( 21 "strings" 22 23 "github.com/pingcap/tidb/parser/mysql" 24 "github.com/pingcap/tidb/parser/terror" 25 ) 26 27 // IsTypeBlob returns a boolean indicating whether the tp is a blob type. 28 func IsTypeBlob(tp byte) bool { 29 switch tp { 30 case mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeBlob, mysql.TypeLongBlob: 31 return true 32 default: 33 return false 34 } 35 } 36 37 // IsTypeChar returns a boolean indicating 38 // whether the tp is the char type like a string type or a varchar type. 39 func IsTypeChar(tp byte) bool { 40 return tp == mysql.TypeString || tp == mysql.TypeVarchar 41 } 42 43 var type2Str = map[byte]string{ 44 mysql.TypeBit: "bit", 45 mysql.TypeBlob: "text", 46 mysql.TypeDate: "date", 47 mysql.TypeDatetime: "datetime", 48 mysql.TypeUnspecified: "unspecified", 49 mysql.TypeNewDecimal: "decimal", 50 mysql.TypeDouble: "double", 51 mysql.TypeEnum: "enum", 52 mysql.TypeFloat: "float", 53 mysql.TypeGeometry: "geometry", 54 mysql.TypeInt24: "mediumint", 55 mysql.TypeJSON: "json", 56 mysql.TypeLong: "int", 57 mysql.TypeLonglong: "bigint", 58 mysql.TypeLongBlob: "longtext", 59 mysql.TypeMediumBlob: "mediumtext", 60 mysql.TypeNull: "null", 61 mysql.TypeSet: "set", 62 mysql.TypeShort: "smallint", 63 mysql.TypeString: "char", 64 mysql.TypeDuration: "time", 65 mysql.TypeTimestamp: "timestamp", 66 mysql.TypeTiny: "tinyint", 67 mysql.TypeTinyBlob: "tinytext", 68 mysql.TypeVarchar: "varchar", 69 mysql.TypeVarString: "var_string", 70 mysql.TypeYear: "year", 71 } 72 73 var str2Type = map[string]byte{ 74 "bit": mysql.TypeBit, 75 "text": mysql.TypeBlob, 76 "date": mysql.TypeDate, 77 "datetime": mysql.TypeDatetime, 78 "unspecified": mysql.TypeUnspecified, 79 "decimal": mysql.TypeNewDecimal, 80 "double": mysql.TypeDouble, 81 "enum": mysql.TypeEnum, 82 "float": mysql.TypeFloat, 83 "geometry": mysql.TypeGeometry, 84 "mediumint": mysql.TypeInt24, 85 "json": mysql.TypeJSON, 86 "int": mysql.TypeLong, 87 "bigint": mysql.TypeLonglong, 88 "longtext": mysql.TypeLongBlob, 89 "mediumtext": mysql.TypeMediumBlob, 90 "null": mysql.TypeNull, 91 "set": mysql.TypeSet, 92 "smallint": mysql.TypeShort, 93 "char": mysql.TypeString, 94 "time": mysql.TypeDuration, 95 "timestamp": mysql.TypeTimestamp, 96 "tinyint": mysql.TypeTiny, 97 "tinytext": mysql.TypeTinyBlob, 98 "varchar": mysql.TypeVarchar, 99 "var_string": mysql.TypeVarString, 100 "year": mysql.TypeYear, 101 } 102 103 // TypeStr converts tp to a string. 104 func TypeStr(tp byte) (r string) { 105 return type2Str[tp] 106 } 107 108 // TypeToStr converts a field to a string. 109 // It is used for converting Text to Blob, 110 // or converting Char to Binary. 111 // Args: 112 // 113 // tp: type enum 114 // cs: charset 115 func TypeToStr(tp byte, cs string) (r string) { 116 ts := type2Str[tp] 117 if cs != "binary" { 118 return ts 119 } 120 if IsTypeBlob(tp) { 121 ts = strings.Replace(ts, "text", "blob", 1) 122 } else if IsTypeChar(tp) { 123 ts = strings.Replace(ts, "char", "binary", 1) 124 } else if tp == mysql.TypeNull { 125 ts = "binary" 126 } 127 return ts 128 } 129 130 // StrToType convert a string to type enum. 131 // Args: 132 // 133 // ts: type string 134 func StrToType(ts string) (tp byte) { 135 ts = strings.Replace(ts, "blob", "text", 1) 136 ts = strings.Replace(ts, "binary", "char", 1) 137 if tp, ok := str2Type[ts]; ok { 138 return tp 139 } 140 141 return mysql.TypeUnspecified 142 } 143 144 var ( 145 dig2bytes = [10]int{0, 1, 1, 2, 2, 3, 3, 4, 4, 4} 146 ) 147 148 // constant values. 149 const ( 150 digitsPerWord = 9 // A word holds 9 digits. 151 wordSize = 4 // A word is 4 bytes int32. 152 ) 153 154 var ( 155 // ErrInvalidDefault is returned when meet a invalid default value. 156 ErrInvalidDefault = terror.ClassTypes.NewStd(mysql.ErrInvalidDefault) 157 // ErrDataOutOfRange is returned when meet a value out of range. 158 ErrDataOutOfRange = terror.ClassTypes.NewStd(mysql.ErrDataOutOfRange) 159 // ErrTruncatedWrongValue is returned when meet a value bigger than 160 // 99999999999999999999999999999999999999999999999999999999999999999 during parsing. 161 ErrTruncatedWrongValue = terror.ClassTypes.NewStd(mysql.ErrTruncatedWrongValue) 162 // ErrIllegalValueForType is returned when strconv.ParseFloat meet strconv.ErrRange during parsing. 163 ErrIllegalValueForType = terror.ClassTypes.NewStd(mysql.ErrIllegalValueForType) 164 )