api2sql基础的能力 #8

Merged
zhangdeman merged 26 commits from feature/api2sql into master 2024-08-24 12:33:47 +08:00
4 changed files with 56 additions and 17 deletions
Showing only changes of commit f359598109 - Show all commits

View File

@ -31,5 +31,7 @@ type IWrapperDatabaseClient interface {
// CacheDataTableStructureConfig 缓存数据表结构的配置
CacheDataTableStructureConfig() *define.CacheTableStructureConfig
// GetTableFieldList 获取指定表数据字段列表
GetTableFieldList(tableName string) ([]*define.ColumnInfo, error)
GetTableFieldList(tableName string) ([]*define.ColumnConfig, error)
// SetTableStructure 设置数据表结构
SetTableStructure(tableConfigTable map[string][]*define.ColumnConfig)
}

View File

@ -70,14 +70,3 @@ type SqlCondition struct {
Operate string `json:"operate"` // 操作 : == / !== / in / not in / like / not like
Value any `json:"value"` // 数据值
}
// ColumnConfig ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:42 2024/8/23
type ColumnConfig struct {
Column string `json:"column"` // 字段名
Alias string `json:"alias"` // 字段别名
Type string `json:"type"` // 字段类型
}

View File

@ -16,3 +16,14 @@ type CacheTableStructureConfig struct {
Enable bool `json:"enable"` // 是否启用表结构缓存
SyncTimeInterval int `json:"sync_time_interval"` // 开启的情况向, 多久同步一次表结构, 默认值 3600, 单位 : s
}
// ColumnConfig ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:42 2024/8/23
type ColumnConfig struct {
Column string `json:"column"` // 字段名
Alias string `json:"alias"` // 字段别名
Type string `json:"type"` // 字段类型
}

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()
}