-
Notifications
You must be signed in to change notification settings - Fork 8
/
table.go
168 lines (158 loc) · 4.63 KB
/
table.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
package redis_orm
import (
"fmt"
"reflect"
"strings"
"sync"
)
/*
SET @table_schema='employees';
SELECT
table_name,
table_type,
engine,
table_rows,
avg_row_length,
data_length,
index_length,
table_collation,
create_time
FROM
information_schema.tables
WHERE
table_schema = @table_schema
ORDER BY table_name;
*/
type SchemaTablesTb struct {
Id int64 `redis_orm:"pk autoincr comment 'ID'"`
TableName string `redis_orm:"unique comment '唯一'"`
TableComment string `redis_orm:"dft '' comment '表注释'"` //暂时没用上
PrimaryKey string `redis_orm:"comment '主键字段'"`
AutoIncrement string `redis_orm:"comment '自增字段'"`
IsSync2DB bool `redis_orm:"comment '是否同步到数据库'"`
Created string `redis_orm:"comment '创建时间字段'"`
Updated string `redis_orm:"comment '更新时间字段'"`
//Version int32 `redis_orm:"comment '版本'"`
CreatedAt int64 `redis_orm:"created_at comment '创建时间'"`
UpdatedAt int64 `redis_orm:"updated_at comment '修改时间'"`
}
func SchemaTablesFromTable(table *Table) *SchemaTablesTb {
return &SchemaTablesTb{
Id: table.TableId,
TableName: table.Name,
TableComment: table.Comment,
PrimaryKey: table.PrimaryKey,
AutoIncrement: table.AutoIncrement,
IsSync2DB: table.IsSync2DB,
Created: table.Created,
Updated: table.Updated,
//Version: table.Version,
}
}
type Table struct {
TableId int64
Name string
Comment string
//Version int32
//Type reflect.Type
ColumnsSeq []string
ColumnsMap map[string]*Column
IndexesMap map[string]*Index
PrimaryKey string
AutoIncrement string
IsSync2DB bool
Created string
Updated string
mutex sync.RWMutex
}
func TableFromSchemaTables(table *SchemaTablesTb) *Table {
return &Table{
TableId: table.Id,
Name: table.TableName,
Comment: table.TableComment,
PrimaryKey: table.PrimaryKey,
AutoIncrement: table.AutoIncrement,
IsSync2DB: table.IsSync2DB,
ColumnsMap: make(map[string]*Column),
IndexesMap: make(map[string]*Index),
Created: table.Created,
Updated: table.Updated,
//Version: table.Version,
}
}
func NewEmptyTable() *Table {
return &Table{Name: "",
ColumnsSeq: make([]string, 0),
ColumnsMap: make(map[string]*Column),
IndexesMap: make(map[string]*Index),
}
}
func (table *Table) GetAutoIncrKey() string {
if table.AutoIncrement != "" {
return fmt.Sprintf("%s%s", KeyAutoIncrPrefix, strings.ToLower(table.AutoIncrement))
} else {
return ""
}
}
func (table *Table) GetTableKey() string {
return fmt.Sprintf("%s%s", KeyTbPrefix, strings.ToLower(table.Name))
}
func (table *Table) AddIndex(typ string, indexColumn, columnName, comment string, isUnique bool, seq byte) {
var indexType IndexType
switch typ {
case reflect.String.String():
indexType = IndexType_IdScore
case reflect.Int.String(), reflect.Int8.String(), reflect.Int16.String(), reflect.Int32.String(), reflect.Int64.String(), reflect.Uint.String(), reflect.Uint8.String():
fallthrough
case reflect.Uint16.String(), reflect.Uint32.String(), reflect.Uint64.String(), reflect.Float32.String(), reflect.Float64.String():
indexType = IndexType_IdMember
case reflect.Uintptr.String(), reflect.Ptr.String():
fallthrough
case reflect.Complex64.String(), reflect.Complex128.String(), reflect.Array.String(), reflect.Chan.String(), reflect.Interface.String(), reflect.Map.String():
fallthrough
case reflect.Slice.String(), reflect.Struct.String(), reflect.Bool.String(), reflect.UnsafePointer.String():
fallthrough
default:
indexType = IndexType_UnSupport
}
if indexType == IndexType_UnSupport {
return
}
if isUnique {
indexType = IndexType_IdScore
}
index := &Index{
NameKey: table.GetIndexKey(columnName),
Seq: seq,
IndexColumn: strings.Split(indexColumn, "&"),
Comment: comment,
Type: indexType,
IsUnique: isUnique,
}
table.mutex.Lock()
table.IndexesMap[strings.ToLower(indexColumn)] = index
table.mutex.Unlock()
}
func (table *Table) GetIndexKey(col string) string {
return fmt.Sprintf("%s%s_%s", KeyIndexPrefix, strings.ToLower(table.Name), strings.ToLower(col))
}
func (table *Table) AddColumn(col *Column) {
if col.IsCombinedIndex {
return
}
table.ColumnsSeq = append(table.ColumnsSeq, col.Name)
colName := col.Name
table.ColumnsMap[colName] = col
if col.IsPrimaryKey {
table.PrimaryKey = col.Name
}
if col.IsAutoIncrement {
table.AutoIncrement = col.Name
}
if col.IsCreated {
table.Created = col.Name
}
if col.IsUpdated {
table.Updated = col.Name
}
}