-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
73 lines (67 loc) · 1.59 KB
/
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
69
70
71
72
73
package converter
import (
"fmt"
"strings"
)
var commonMysqlDataTypeMap = map[string]string{
// For consistency, all integer types are converted to int64
// number
"bool": "int64",
"boolean": "int64",
"tinyint": "int64",
"smallint": "int64",
"mediumint": "int64",
"int": "int64",
"integer": "int64",
"bigint": "int64",
"float": "float64",
"double": "float64",
"decimal": "float64",
// date&time
"date": "time.Time",
"datetime": "time.Time",
"timestamp": "time.Time",
"time": "string",
"year": "int64",
// string
"char": "string",
"varchar": "string",
"binary": "string",
"varbinary": "string",
"tinytext": "string",
"text": "string",
"mediumtext": "string",
"longtext": "string",
"enum": "string",
"set": "string",
"json": "string",
}
// ConvertDataType converts mysql column type into golang type
func ConvertDataType(dataBaseType string, isDefaultNull bool) (string, error) {
tp, ok := commonMysqlDataTypeMap[strings.ToLower(dataBaseType)]
if !ok {
return "", fmt.Errorf("unexpected database type: %s", dataBaseType)
}
return mayConvertNullType(tp, isDefaultNull), nil
}
func mayConvertNullType(goDataType string, isDefaultNull bool) string {
if !isDefaultNull {
return goDataType
}
switch goDataType {
case "int64":
return "sql.NullInt64"
case "int32":
return "sql.NullInt32"
case "float64":
return "sql.NullFloat64"
case "bool":
return "sql.NullBool"
case "string":
return "sql.NullString"
case "time.Time":
return "sql.NullTime"
default:
return goDataType
}
}