支持人工设置表结构配置

This commit is contained in:
2024-08-23 17:29:11 +08:00
parent 5ed8f2007e
commit f359598109
4 changed files with 56 additions and 17 deletions

View File

@ -19,6 +19,7 @@ import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
"strings"
"sync"
"time"
)
@ -37,7 +38,7 @@ type DBClient struct {
Cfg define.Driver // 数据库配置
cacheTableStructureConfig *define.CacheTableStructureConfig // 缓存配置
lock *sync.RWMutex // 操作锁
tableStructureCache map[string][]*define.ColumnInfo // 表结构缓存
tableStructureCache map[string][]*define.ColumnConfig // 表结构缓存
}
// Init 初始化客户端
@ -179,10 +180,10 @@ func (dc *DBClient) CacheDataTableStructureConfig() *define.CacheTableStructureC
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:07 2024/8/21
func (dc *DBClient) GetTableFieldList(tableName string) ([]*define.ColumnInfo, error) {
func (dc *DBClient) GetTableFieldList(tableName string) ([]*define.ColumnConfig, error) {
if !dc.CacheDataTableStructureConfig().Enable {
// 未启用缓存, 返回空list
return make([]*define.ColumnInfo, 0), nil
return make([]*define.ColumnConfig, 0), nil
}
dc.lock.RLock()
defer dc.lock.RUnlock()
@ -198,6 +199,10 @@ func (dc *DBClient) GetTableFieldList(tableName string) ([]*define.ColumnInfo, e
//
// Date : 15:17 2024/8/21
func (dc *DBClient) syncDbTableStructure(ignoreErr bool) error {
if !dc.CacheDataTableStructureConfig().Enable {
// 自动同步不可用
return nil
}
var (
err error
tableList []string
@ -208,7 +213,7 @@ func (dc *DBClient) syncDbTableStructure(ignoreErr bool) error {
if tableList, err = systemDao.GetTableList(c); nil != err {
return err
}
tableStructCache := make(map[string][]*define.ColumnInfo)
tableStructCache := make(map[string][]*define.ColumnConfig)
for _, itemTableName := range tableList {
fieldList, loadTableErr := systemDao.GetTableInfo(c, dc.Cfg.Database, itemTableName)
if nil != loadTableErr {
@ -217,7 +222,22 @@ func (dc *DBClient) syncDbTableStructure(ignoreErr bool) error {
}
return loadTableErr
}
tableStructCache[itemTableName] = fieldList
tableStructCache[itemTableName] = make([]*define.ColumnConfig, 0)
for _, itemColumn := range fieldList {
fieldType := "string"
if strings.Contains(itemColumn.ColumnType, "int") {
if strings.Contains(itemColumn.ColumnType, "unsigned") {
fieldType = "uint"
} else {
fieldType = "int"
}
}
tableStructCache[itemTableName] = append(tableStructCache[itemTableName], &define.ColumnConfig{
Column: itemColumn.ColumnName,
Alias: itemColumn.ColumnName,
Type: fieldType,
})
}
}
// 更新缓存结果
dc.lock.Lock()
@ -225,3 +245,20 @@ func (dc *DBClient) syncDbTableStructure(ignoreErr bool) error {
dc.tableStructureCache = tableStructCache
return nil
}
// SetTableStructure 设置表结构, 一旦调用人工设置, 则将终止自动同步
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:06 2024/8/23
func (dc *DBClient) SetTableStructure(tableConfigTable map[string][]*define.ColumnConfig) {
if nil != dc.cacheTableStructureConfig {
// 关闭自动同步
dc.cacheTableStructureConfig.Enable = false
}
dc.lock.Lock()
for table, columnConfig := range tableConfigTable {
dc.tableStructureCache[table] = columnConfig
}
dc.lock.Unlock()
}