-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.go
More file actions
130 lines (113 loc) · 3.77 KB
/
meta.go
File metadata and controls
130 lines (113 loc) · 3.77 KB
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
package daogext
import (
"fmt"
"reflect"
"strings"
dgcoll "github.com/darwinOrg/go-common/collection"
dgctx "github.com/darwinOrg/go-common/context"
dgsys "github.com/darwinOrg/go-common/sys"
dglogger "github.com/darwinOrg/go-logger"
"github.com/rolandhe/daog"
)
type columnInfo struct {
TableName string
ColumnName string
ColumnType string // 如: int(11) unsigned
}
type tableMetaExt struct {
Table string
Columns []string
Types []string
}
var tableMetaExts []*tableMetaExt
func NewBaseQuickDao[T any](meta *daog.TableMeta[T]) daog.QuickDao[T] {
tableMetaExts = append(tableMetaExts, convertToMetaExt(meta))
return daog.NewBaseQuickDao(meta)
}
func convertToMetaExt[T any](meta *daog.TableMeta[T]) *tableMetaExt {
obj := new(T)
var types []string
for _, column := range meta.Columns {
fieldObj := meta.LookupFieldFunc(column, obj, true)
fieldType := reflect.TypeOf(fieldObj)
for fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
}
fieldTypeString := fieldType.String()
types = append(types, fieldTypeString)
}
return &tableMetaExt{
Table: meta.Table,
Columns: meta.Columns,
Types: types,
}
}
func validateTableMeta() {
if !dgsys.IsFormalProfile() || len(tableMetaExts) == 0 {
return
}
tableMetaExtMap := dgcoll.Trans2Map(tableMetaExts, func(meta *tableMetaExt) string { return meta.Table })
tablesNames := dgcoll.MapToList(tableMetaExts, func(meta *tableMetaExt) string { return meta.Table })
tablesNamesStr := "'" + strings.Join(tablesNames, "','") + "'"
queryColumnSql := fmt.Sprintf(`
SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME in (%s)
ORDER BY TABLE_NAME, ORDINAL_POSITION
`, tablesNamesStr)
ctx := dgctx.SimpleDgContext()
columnInfos, err := ReadonlyWithResult(ctx, func(tc *daog.TransContext) ([]*columnInfo, error) {
list, err := daog.QueryRawSQL(tc, func(ins *columnInfo) []any {
return []any{&ins.TableName, &ins.ColumnName, &ins.ColumnType}
}, queryColumnSql)
if err != nil {
dglogger.Errorf(ctx, "查询数据库列元信息错误: %v", err)
return nil, err
}
return list, nil
})
if err != nil {
return
}
existsTableNames := dgcoll.MapToSet(columnInfos, func(info *columnInfo) string { return info.TableName })
notExistsTableNames := dgcoll.Remove(tablesNames, existsTableNames)
if len(notExistsTableNames) > 0 {
dbe := fmt.Errorf("错误!数据库缺少表: %s", strings.Join(notExistsTableNames, ", "))
if errorProcessor != nil {
errorProcessor(ctx, dbe)
} else {
dglogger.Warn(ctx, dbe)
}
}
tableName2ColumnInfosMap := dgcoll.GroupBy(columnInfos, func(info *columnInfo) string { return info.TableName })
for tableName, tableColumnInfos := range tableName2ColumnInfosMap {
metaExt := tableMetaExtMap[tableName]
metaColumns := metaExt.Columns
for i, metaColumn := range metaColumns {
tableColumnInfo := dgcoll.FindFirst(tableColumnInfos, func(info *columnInfo) bool { return info.ColumnName == metaColumn }, nil)
// 如果实际数据库里面没有这个字段,则报警
if tableColumnInfo == nil {
dbe := fmt.Errorf("错误![%s.%s]字段缺失", tableName, metaColumn)
if errorProcessor != nil {
errorProcessor(ctx, dbe)
} else {
dglogger.Warn(ctx, dbe)
}
continue
}
metaColumnType := metaExt.Types[i]
dbColumnType := tableColumnInfo.ColumnType
// 如果mysql与go的数据类型不匹配,则报警
if !isMySQLTypeCompatibleWithGo(dbColumnType, metaColumnType) {
dbe := fmt.Errorf("错误![%s.%s]字段类型不匹配: %s / %s", tableName, metaColumn, dbColumnType, metaColumnType)
if errorProcessor != nil {
errorProcessor(ctx, dbe)
} else {
dglogger.Warn(ctx, dbe)
}
continue
}
}
}
}