-
Notifications
You must be signed in to change notification settings - Fork 269
/
mysql_types.go
68 lines (59 loc) · 2.16 KB
/
mysql_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2023 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package utils
import (
"strings"
timodel "github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/parser/types"
)
// WithUnsigned4MySQLType add `unsigned` keyword.
// it should have the form `t unsigned`, such as `int unsigned`
func WithUnsigned4MySQLType(mysqlType string, unsigned bool) string {
if unsigned && mysqlType != "bit" && mysqlType != "year" {
return mysqlType + " unsigned"
}
return mysqlType
}
// WithZerofill4MySQLType add `zerofill` keyword.
func WithZerofill4MySQLType(mysqlType string, zerofill bool) string {
if zerofill &&
!strings.HasPrefix(mysqlType, "bit") &&
!strings.HasPrefix(mysqlType, "year") {
return mysqlType + " zerofill"
}
return mysqlType
}
// GetMySQLType get the mysql type from column info
func GetMySQLType(columnInfo *timodel.ColumnInfo, fullType bool) string {
if !fullType {
result := types.TypeToStr(columnInfo.GetType(), columnInfo.GetCharset())
result = withUnsigned4MySQLType(result, mysql.HasUnsignedFlag(columnInfo.GetFlag()))
result = withZerofill4MySQLType(result, mysql.HasZerofillFlag(columnInfo.GetFlag()))
return result
}
return columnInfo.GetTypeDesc()
}
// ExtractBasicMySQLType return the mysql type
func ExtractBasicMySQLType(mysqlType string) byte {
for i := 0; i < len(mysqlType); i++ {
if mysqlType[i] == '(' || mysqlType[i] == ' ' {
return types.StrToType(mysqlType[:i])
}
}
return types.StrToType(mysqlType)
}
// IsBinaryMySQLType return true if the given mysqlType string is a binary type
func IsBinaryMySQLType(mysqlType string) bool {
return strings.Contains(mysqlType, "blob") || strings.Contains(mysqlType, "binary")
}