-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
241 lines (205 loc) · 5.07 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package loaders
import (
"strings"
_ "github.com/go-sql-driver/mysql"
"github.com/knq/snaker"
"github.com/knq/xo/internal"
"github.com/knq/xo/models"
)
func init() {
internal.SchemaLoaders["mysql"] = internal.TypeLoader{
ParamN: func(int) string { return "?" },
MaskFunc: func() string { return "?" },
ProcessRelkind: MyRelkind,
Schema: MySchema,
ParseType: MyParseType,
EnumList: models.MyEnums,
EnumValueList: MyEnumValues,
ProcList: models.MyProcs,
ProcParamList: models.MyProcParams,
TableList: models.MyTables,
ColumnList: models.MyTableColumns,
ForeignKeyList: models.MyTableForeignKeys,
IndexList: models.MyTableIndexes,
IndexColumnList: models.MyIndexColumns,
QueryColumnList: MyQueryColumns,
}
}
// MySchema retrieves the name of the current schema.
func MySchema(args *internal.ArgType) (string, error) {
var err error
// sql query
const sqlstr = `SELECT SCHEMA()`
var schema string
// run query
models.XOLog(sqlstr)
err = args.DB.QueryRow(sqlstr).Scan(&schema)
if err != nil {
return "", err
}
return schema, nil
}
// MyRelkind returns the mysql string representation for RelType.
func MyRelkind(relType internal.RelType) string {
var s string
switch relType {
case internal.Table:
s = "BASE TABLE"
case internal.View:
s = "VIEW"
default:
panic("unsupported RelType")
}
return s
}
// MyParseType parse a mysql type into a Go type based on the column
// definition.
func MyParseType(args *internal.ArgType, dt string, nullable bool) (int, string, string) {
precision := 0
nilVal := "nil"
unsigned := false
// extract unsigned
if strings.HasSuffix(dt, " unsigned") {
unsigned = true
dt = dt[:len(dt)-len(" unsigned")]
}
// extract precision
dt, precision, _ = args.ParsePrecision(dt)
var typ string
switchDT:
switch dt {
case "bit":
nilVal = "0"
if precision == 1 {
nilVal = "false"
typ = "bool"
if nullable {
nilVal = "sql.NullBool{}"
typ = "sql.NullBool"
}
break switchDT
} else if precision <= 8 {
typ = "uint8"
} else if precision <= 16 {
typ = "uint16"
} else if precision <= 32 {
typ = "uint32"
} else {
typ = "uint64"
}
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "bool", "boolean":
nilVal = "false"
typ = "bool"
if nullable {
nilVal = "sql.NullBool{}"
typ = "sql.NullBool"
}
case "char", "varchar", "tinytext", "text", "mediumtext", "longtext":
nilVal = `""`
typ = "string"
if nullable {
nilVal = "sql.NullString{}"
typ = "sql.NullString"
}
case "tinyint", "smallint":
nilVal = "0"
typ = "int16"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "mediumint", "int", "integer":
nilVal = "0"
typ = args.Int32Type
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "bigint":
nilVal = "0"
typ = "int64"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "float":
nilVal = "0.0"
typ = "float32"
if nullable {
nilVal = "sql.NullFloat64{}"
typ = "sql.NullFloat64"
}
case "decimal", "double":
nilVal = "0.0"
typ = "float64"
if nullable {
nilVal = "sql.NullFloat64{}"
typ = "sql.NullFloat64"
}
case "binary", "varbinary", "tinyblob", "blob", "mediumblob", "longblob":
typ = "[]byte"
case "timestamp", "datetime", "date", "time":
typ = "*time.Time"
if nullable {
nilVal = "mysql.NullTime{}"
typ = "mysql.NullTime"
}
default:
if strings.HasPrefix(dt, args.Schema+".") {
// in the same schema, so chop off
typ = snaker.SnakeToCamelIdentifier(dt[len(args.Schema)+1:])
nilVal = typ + "(0)"
} else {
typ = snaker.SnakeToCamelIdentifier(dt)
nilVal = typ + "{}"
}
}
// add 'u' as prefix to type if its unsigned
// FIXME: this needs to be tested properly...
if unsigned && internal.IntRE.MatchString(typ) {
typ = "u" + typ
}
return precision, nilVal, typ
}
// MyEnumValues loads the enum values.
func MyEnumValues(db models.XODB, schema string, enum string) ([]*models.EnumValue, error) {
var err error
// load enum vals
res, err := models.MyEnumValues(db, schema, enum)
if err != nil {
return nil, err
}
// process enum vals
enumVals := []*models.EnumValue{}
for i, ev := range strings.Split(res.EnumValues[1:len(res.EnumValues)-1], "','") {
enumVals = append(enumVals, &models.EnumValue{
EnumValue: ev,
ConstValue: i + 1,
})
}
return enumVals, nil
}
// MyQueryColumns parses the query and generates a type for it.
func MyQueryColumns(args *internal.ArgType, inspect []string) ([]*models.Column, error) {
var err error
// create temporary view xoid
xoid := "_xo_" + internal.GenRandomID()
viewq := `CREATE VIEW ` + xoid + ` AS (` + strings.Join(inspect, "\n") + `)`
models.XOLog(viewq)
_, err = args.DB.Exec(viewq)
if err != nil {
return nil, err
}
// load columns
cols, err := models.MyTableColumns(args.DB, args.Schema, xoid)
// drop inspect view
dropq := `DROP VIEW ` + xoid
models.XOLog(dropq)
_, _ = args.DB.Exec(dropq)
// load column information
return cols, err
}