github.com/XiaoMi/Gaea@v1.2.5/parser/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/XiaoMi/Gaea/mysql" 24 "github.com/XiaoMi/Gaea/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.TypeDecimal: "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 // TypeStr converts tp to a string. 74 func TypeStr(tp byte) (r string) { 75 return type2Str[tp] 76 } 77 78 // TypeToStr converts a field to a string. 79 // It is used for converting Text to Blob, 80 // or converting Char to Binary. 81 // Args: 82 // tp: type enum 83 // cs: charset 84 func TypeToStr(tp byte, cs string) (r string) { 85 ts := type2Str[tp] 86 if cs != "binary" { 87 return ts 88 } 89 if IsTypeBlob(tp) { 90 ts = strings.Replace(ts, "text", "blob", 1) 91 } else if IsTypeChar(tp) { 92 ts = strings.Replace(ts, "char", "binary", 1) 93 } 94 return ts 95 } 96 97 var ( 98 dig2bytes = [10]int{0, 1, 1, 2, 2, 3, 3, 4, 4, 4} 99 ) 100 101 // constant values. 102 const ( 103 digitsPerWord = 9 // A word holds 9 digits. 104 wordSize = 4 // A word is 4 bytes int32. 105 ) 106 107 const ( 108 codeInvalidDefault = terror.ErrCode(mysql.ErrInvalidDefault) 109 ) 110 111 // ErrInvalidDefault is returned when meet a invalid default value. 112 var ErrInvalidDefault = terror.ClassTypes.New(codeInvalidDefault, "Invalid default value for '%s'")