-
Notifications
You must be signed in to change notification settings - Fork 1
/
desc.go
272 lines (243 loc) · 6.98 KB
/
desc.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package mysqlx
import (
"database/sql"
"fmt"
"reflect"
"strings"
)
/* reference:
SELECT ORDINAL_POSITION as No,
COLUMN_NAME as `Field`,
COLUMN_TYPE as `Type`,
IS_NULLABLE as `Nullable`,
COLUMN_DEFAULT as `Default`,
COLUMN_KEY as `Key`,
COLUMN_COMMENT as `Comment`,
EXTRA as `Extra`
FROM information_schema.columns
WHERE table_schema='db_test' and table_name='t_room_bill_binding' ORDER BY ORDINAL_POSITION;
*/
type _Field struct {
// No int `db:"No"`
Field string `db:"Field"`
Type string `db:"Type"`
Nullable string `db:"Null"`
Default sql.NullString `db:"Default"`
Key string `db:"Key"`
Extra string `db:"Extra"`
// Comment string `db:"Comment"`
}
// const _ReadTableFields = `
// SELECT ORDINAL_POSITION as ` + "`No`" + `,
// COLUMN_NAME as ` + "`Field`" + `,
// COLUMN_TYPE as ` + "`Type`" + `,
// IS_NULLABLE as ` + "`Null`" + `,
// COLUMN_DEFAULT as ` + "`Default`" + `,
// COLUMN_KEY as ` + "`Key`" + `,
// EXTRA as ` + "`Extra`" + `,
// COLUMN_COMMENT as ` + "`Comment`" + `
// FROM ` + "`columns`" + `
// WHERE table_schema='%s' and table_name='%s' ORDER BY ORDINAL_POSITION;
// `
// const _ReadTableFields = "desc `%s`"
const _ReadTableFields = `SELECT
COLUMN_NAME as ` + "`Field`" + `,
COLUMN_TYPE as ` + "`Type`" + `,
IS_NULLABLE as ` + "`Null`" + `,
COLUMN_DEFAULT as ` + "`Default`" + `,
COLUMN_KEY as ` + "`Key`" + `,
EXTRA as ` + "`Extra`" + `
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s' ORDER BY ORDINAL_POSITION`
// ReadTableFields returns all fields in given table
func (d *xdb) ReadTableFields(table string) (ret []*Field, err error) {
if nil == d.db {
return nil, fmt.Errorf("mysqlx not initialized")
}
if "" == table {
return nil, fmt.Errorf("empty table name")
}
database := d.param.DBName
if "" == database {
var err error
database, err = d.CurrentDatabase()
if err != nil {
return nil, err
}
}
// query := fmt.Sprintf(_ReadTableFields, table)
query := fmt.Sprintf(_ReadTableFields, database, table)
var fields []*_Field
err = d.db.Select(&fields, query)
if err != nil {
return nil, newError(err.Error(), query)
}
if nil == fields || 0 == len(fields) {
// return make([]*Field, 0), nil
e := fmt.Sprintf("Table '%s.%s' doesn't exist", database, table)
return nil, newError(e, query)
}
ret = make([]*Field, 0, len(fields))
for _, f := range fields {
retF := Field{
Name: f.Field,
Type: f.Type,
// Comment: f.Comment,
}
// nullable
switch strings.ToUpper(f.Nullable) {
case "YES", "TRUE":
retF.Nullable = true
default:
retF.Nullable = false
}
// Default
if f.Default.Valid {
if strings.Contains(f.Type, "char") || strings.Contains(f.Type, "text") {
retF.Default = "'" + strings.Replace(f.Default.String, "'", "\\'", -1) + "'"
} else {
retF.Default = f.Default.String
}
} else {
retF.Default = "NULL"
}
// auto_increment
if strings.Contains(f.Extra, "auto_increment") {
retF.AutoIncrement = true
}
// append
ret = append(ret, &retF)
}
return
}
type currDB struct {
Database string `db:"database()"`
}
// CurrentDatabase gets current operating database
func (d *xdb) CurrentDatabase() (string, error) {
var res []currDB
err := d.db.Select(&res, "select database()")
if err != nil {
return "", err
}
if nil == res || 0 == len(res) {
return "", fmt.Errorf("current database unknown")
}
return res[0].Database, nil
}
// ========
type _Index struct {
Table string `db:"TABLE_NAME"`
NonUnique int `db:"NON_UNIQUE"`
KeyName string `db:"INDEX_NAME"`
SeqInIndex int `db:"SEQ_IN_INDEX"`
ColumnName string `db:"COLUMN_NAME"`
// Collation string `db:"Collation"`
// Cardinality string `db:"Cardinality"`
// SubPart sql.NullInt64 `db:"Sub_part"`
// Packed sql.NullString `db:"Packed"`
Null sql.NullString `db:"NULLABLE"`
// IndexType string `db:"Index_type"`
// Comment string `db:"Comment"`
// IndexComment string `db:"Index_comment"`
// Visible string `db:"Visible"`
// Expression sql.NullString `db:"Expression"`
}
// const _ReadTableIndexes = "show index from `%s`"
const _ReadTableIndexes = "SELECT TABLE_NAME, NON_UNIQUE, INDEX_NAME, SEQ_IN_INDEX, COLUMN_NAME, NULLABLE FROM information_schema.STATISTICS WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s'"
// ReadTableIndexes returns all indexes and uniques of given table name
func (d *xdb) ReadTableIndexes(table string) (map[string]*Index, map[string]*Unique, error) {
if nil == d.db {
return nil, nil, fmt.Errorf("mysqlx not initialized")
}
if "" == table {
return nil, nil, fmt.Errorf("empty table name")
}
database := d.param.DBName
if "" == database {
var err error
database, err = d.CurrentDatabase()
if err != nil {
return nil, nil, err
}
}
var err error
var indexes []*_Index
query := fmt.Sprintf(_ReadTableIndexes, database, table)
err = d.db.Select(&indexes, query)
if err != nil {
return nil, nil, newError(err.Error(), query)
}
indexMap := make(map[string]*Index)
uniqueMap := make(map[string]*Unique)
if nil == indexes || 0 == len(indexes) {
return indexMap, uniqueMap, nil
}
for _, idx := range indexes {
if strings.ToUpper(idx.KeyName) == "PRIMARY" {
continue
}
if idx.NonUnique > 0 {
// Jut a normal index
index, exist := indexMap[idx.KeyName]
if false == exist {
index = &Index{
Name: idx.KeyName,
Fields: make([]string, 0),
}
indexMap[idx.KeyName] = index
}
index.Fields = append(index.Fields, idx.ColumnName)
} else {
// unique
unique, exist := uniqueMap[idx.KeyName]
if false == exist {
unique = &Unique{
Name: idx.KeyName,
Fields: make([]string, 0),
}
uniqueMap[idx.KeyName] = unique
}
unique.Fields = append(unique.Fields, idx.ColumnName)
}
}
return indexMap, uniqueMap, nil
}
// ReadStructFields returns all valid SQL fields by given structure and will buffer it
func (d *xdb) ReadStructFields(s interface{}) (ret []*Field, err error) {
// read from buffer
intfName := reflect.TypeOf(s)
fieldValue, exist := d.bufferedFields.Load(intfName)
if exist {
return fieldValue.([]*Field), nil
}
// read now
ret, err = ReadStructFields(s)
if err != nil {
d.bufferedFields.Store(intfName, ret)
}
return
}
// StructFields is the same as ReadStructFields
func (*xdb) StructFields(s interface{}) (ret []*Field, err error) {
return ReadStructFields(s)
}
// StructFields returns all valid SQL fields by given structure
func StructFields(s interface{}) (ret []*Field, err error) {
return ReadStructFields(s)
}
// ReadStructFields is the same as StructFields
func ReadStructFields(s interface{}) (ret []*Field, err error) {
t := reflect.TypeOf(s)
v := reflect.ValueOf(s)
switch t.Kind() {
case reflect.Ptr:
return ReadStructFields(v.Elem().Interface())
case reflect.Struct:
// OK, continue
default:
err = fmt.Errorf("invalid type: %v", t.Kind())
return
}
return readStructFields(t, v)
}