/
mysql_type.go
106 lines (93 loc) · 2.51 KB
/
mysql_type.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
package mysql
import (
"gopkg.in/doug-martin/goqu.v4"
)
// DataBaseMetadata metadata of a database
type DataBaseMetadata struct {
DatabaseName string // database name
Tables []*TableMetadata // collection of tables
}
// TableMetadata metadata of a table
type TableMetadata struct {
TableName string //table name
Columns []*ColumnMetadata //collections of column
}
// ColumnMetadata metadata of a column
type ColumnMetadata struct {
ColumnName string // column name or code ?
ColumnType string // column type
NullAble string // column null able
// If Key is PRI, the column is a PRIMARY KEY or is one of the
// columns in a multiple-column PRIMARY KEY.
// If Key is UNI, the column is the first column of a unique-valued
// index that cannot contain NULL values.
// If Key is MUL, multiple occurrences of a given value are
// permitted within the column. The column is the first column
// of a nonunique index or a unique-valued index that can contain
// NULL values.
Key string // column key type
DefaultValue string // default value if have
Extra string // extra info, for example, auto_increment
}
// QueryConfig for Select method
type QueryOption struct {
limit int
offset int
fields []interface{}
links []interface{}
wheres map[string]goqu.Op
}
// GetTableMeta
func (d *DataBaseMetadata) GetTableMeta(tableName string) *TableMetadata {
for _, table := range d.Tables {
if table.TableName == tableName {
return table
}
}
return nil
}
// GetSimpleMetadata
func (d *DataBaseMetadata) GetSimpleMetadata() (rt map[string]map[string]string) {
rt = make(map[string]map[string]string)
for _, table := range d.Tables {
t := make(map[string]string)
for _, f := range table.Columns {
t[f.ColumnName] = f.ColumnType
}
rt[table.TableName] = t
}
return
}
// GetPrimaryColumn
func (t *TableMetadata) GetPrimaryColumn() *ColumnMetadata {
for _, col := range t.Columns {
if col.Key == "PRI" {
return col
}
}
return nil
}
// HaveField
func (t *TableMetadata) HaveField(sFieldName string) bool {
for _, col := range t.Columns {
if sFieldName == col.ColumnName {
return true
}
}
return false
}
// HaveTable
func (d *DataBaseMetadata) HaveTable(sTableName string) bool {
if t := d.GetTableMeta(sTableName); t != nil {
return true
}
return false
}
// TableHaveField
func (d *DataBaseMetadata) TableHaveField(sTableName string, sFieldName string) bool {
t := d.GetTableMeta(sTableName)
if t == nil {
return false
}
return t.HaveField(sFieldName)
}