forked from xo/xo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
151 lines (145 loc) · 4.21 KB
/
mysql.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package loader
import (
"context"
"regexp"
"strings"
"github.com/xo/xo/models"
xo "github.com/xo/xo/types"
)
func init() {
Register("mysql", Loader{
Mask: "?",
Schema: models.MysqlSchema,
Enums: models.MysqlEnums,
EnumValues: MysqlEnumValues,
Procs: models.MysqlProcs,
ProcParams: models.MysqlProcParams,
Tables: models.MysqlTables,
TableColumns: models.MysqlTableColumns,
TableSequences: models.MysqlTableSequences,
TableForeignKeys: models.MysqlTableForeignKeys,
TableIndexes: models.MysqlTableIndexes,
IndexColumns: models.MysqlIndexColumns,
ViewCreate: models.MysqlViewCreate,
ViewDrop: models.MysqlViewDrop,
})
}
// MysqlGoType parse a mysql type into a Go type based on the column
// definition.
func MysqlGoType(d xo.Type, schema, itype, utype string) (string, string, error) {
var goType, zero string
switch d.Type {
case "bit":
switch {
case d.Prec == 1 && !d.Nullable:
goType, zero = "bool", "false"
case d.Prec == 1 && d.Nullable:
goType, zero = "sql.NullBool", "sql.NullBool{}"
case d.Prec <= 8 && !d.Nullable:
goType, zero = "uint8", "0"
case d.Prec <= 16 && !d.Nullable:
goType, zero = "uint16", "0"
case d.Prec <= 32 && !d.Nullable:
goType, zero = "uint32", "0"
case d.Nullable:
goType, zero = "sql.NullInt64", "sql.NullInt64{}"
default:
goType, zero = "uint64", "0"
}
case "bool", "boolean":
goType, zero = "bool", "false"
if d.Nullable {
goType, zero = "sql.NullBool", "sql.NullBool{}"
}
case "char", "varchar", "tinytext", "text", "mediumtext", "longtext":
goType, zero = "string", `""`
if d.Nullable {
goType, zero = "sql.NullString", "sql.NullString{}"
}
case "tinyint":
switch {
case d.Prec == 1 && !d.Nullable: // force tinyint(1) as bool
goType, zero = "bool", "false"
case d.Prec == 1 && d.Nullable:
goType, zero = "sql.NullBool", "sql.NullBool{}"
case d.Nullable:
goType, zero = "sql.NullInt64", "sql.NullInt64{}"
default:
goType, zero = "int8", "0"
}
case "smallint", "year":
goType, zero = "int16", "0"
if d.Nullable {
goType, zero = "sql.NullInt64", "sql.NullInt64{}"
}
case "mediumint", "int", "integer":
goType, zero = itype, "0"
if d.Nullable {
goType, zero = "sql.NullInt64", "sql.NullInt64{}"
}
case "bigint":
goType, zero = "int64", "0"
if d.Nullable {
goType, zero = "sql.NullInt64", "sql.NullInt64{}"
}
case "float":
goType, zero = "float32", "0.0"
if d.Nullable {
goType, zero = "sql.NullFloat64", "sql.NullFloat64{}"
}
case "decimal", "double":
goType, zero = "float64", "0.0"
if d.Nullable {
goType, zero = "sql.NullFloat64", "sql.NullFloat64{}"
}
case "binary", "blob", "longblob", "mediumblob", "tinyblob", "varbinary":
goType, zero = "[]byte", "nil"
case "json":
goType, zero = "json.RawMessage", "nil"
case "timestamp", "datetime", "date":
goType, zero = "time.Time", "time.Time{}"
if d.Nullable {
goType, zero = "sql.NullTime", "sql.NullTime{}"
}
case "time":
// time is not supported by the MySQL driver. Can parse the string to time.Time in the user code.
goType, zero = "string", `""`
if d.Nullable {
goType, zero = "sql.NullString", "sql.NullString{}"
}
default:
goType, zero = schemaType(d.Type, d.Nullable, schema)
}
// force []byte for SET('a',...)
if setRE.MatchString(d.Type) {
goType, zero = "[]byte", "nil"
}
// if unsigned ...
if intRE.MatchString(goType) && d.Unsigned {
if goType == itype {
goType, zero = utype, "0"
} else {
goType = "u" + goType
}
}
return goType, zero, nil
}
// setRE is the regexp that matches MySQL SET() type definitions.
var setRE = regexp.MustCompile(`(?i)^set\([^)]*\)$`)
// MysqlEnumValues loads the enum values.
func MysqlEnumValues(ctx context.Context, db models.DB, schema string, enum string) ([]*models.EnumValue, error) {
// load enum vals
res, err := models.MysqlEnumValues(ctx, db, schema, enum)
if err != nil {
return nil, err
}
// process enum vals
var values []*models.EnumValue
for i, val := range strings.Split(res.EnumValues[1:len(res.EnumValues)-1], "','") {
values = append(values, &models.EnumValue{
EnumValue: val,
ConstValue: i + 1,
})
}
return values, nil
}