-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcolumn.go
98 lines (89 loc) · 2.74 KB
/
column.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
package redis_orm
import (
"strings"
)
type SchemaColumnsTb struct {
Id int64 `redis_orm:"pk autoincr comment 'ID'"`
TableId int64 `redis_orm:"index comment '表ID'"`
Seq byte `redis_orm:"comment '列顺序'"`
ColumnName string `redis_orm:"comment '列名'"`
ColumnComment string `redis_orm:"dft '' comment '列注释'"`
DataType string `redis_orm:"comment '数据类型'"`
DefaultValue string `redis_orm:"comment '默认值'"`
TableIdColumnName string `redis_orm:"combinedindex TableId&ColumnName comment '组合索引(表ID&列名)'"`
CreatedAt int64 `redis_orm:"created_at comment '创建时间'"`
UpdatedAt int64 `redis_orm:"updated_at comment '修改时间'"`
}
func SchemaColumnsFromColumn(tableId int64, v *Column) *SchemaColumnsTb {
return &SchemaColumnsTb{
Seq: v.Seq,
TableId: tableId,
ColumnName: v.Name,
ColumnComment: v.Comment,
DefaultValue: v.DefaultValue,
DataType: v.DataType,
}
}
type Column struct {
Seq byte
Name string
DefaultValue string
IsPrimaryKey bool
IsAutoIncrement bool
IsCreated bool
IsUpdated bool
IsCombinedIndex bool //it 's only used for judge wherther need insert or delete and so on
//IsCascade bool
EnumOptions map[string]int
//SetOptions map[string]int
Comment string
DataType string
//Type reflect.Type //only support base type
}
func ColumnFromSchemaColumns(v *SchemaColumnsTb, schemaTable *SchemaTablesTb) *Column {
column := &Column{
Seq: v.Seq,
Name: v.ColumnName,
Comment: v.ColumnComment,
DefaultValue: v.DefaultValue,
DataType: v.DataType,
//Type:reflect.Type() todo:type的支持
}
if schemaTable.PrimaryKey == v.ColumnName {
column.IsPrimaryKey = true
}
if schemaTable.AutoIncrement == v.ColumnName {
column.IsAutoIncrement = true
}
if schemaTable.Created == v.ColumnName {
column.IsCreated = true
}
if schemaTable.Updated == v.ColumnName {
column.IsUpdated = true
}
if strings.HasPrefix(v.DataType, TagEnum) {
column.EnumOptions = make(map[string]int)
enumVal := strings.TrimPrefix(v.DataType, TagEnum)
enumVal = strings.TrimPrefix(enumVal, "(")
enumVal = strings.TrimSuffix(enumVal, ")")
enumAry := strings.Split(enumVal, ",")
for index, item := range enumAry {
column.EnumOptions[item] = index
}
}
return column
}
func NewEmptyColumn(colName string) *Column {
return &Column{Name: colName, IsPrimaryKey: false,
IsAutoIncrement: false}
}
type ColumnsModel []*Column
func (c ColumnsModel) Len() int {
return len(c)
}
func (c ColumnsModel) Less(i, j int) bool {
return c[i].Seq < c[j].Seq
}
func (c ColumnsModel) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}