database/api2sql/execute.go

282 lines
8.0 KiB
Go
Raw Normal View History

2024-08-21 21:00:11 +08:00
// Package api2sql ...
//
// Description : api2sql ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-08-21 20:45
package api2sql
import (
"context"
"errors"
2024-08-23 16:50:47 +08:00
"fmt"
2024-08-21 21:00:11 +08:00
"git.zhangdeman.cn/zhangdeman/consts"
2024-08-23 16:50:47 +08:00
"git.zhangdeman.cn/zhangdeman/database"
2024-08-21 21:11:19 +08:00
"git.zhangdeman.cn/zhangdeman/database/abstract"
2024-08-21 21:00:11 +08:00
"git.zhangdeman.cn/zhangdeman/database/define"
2024-08-22 11:46:00 +08:00
"git.zhangdeman.cn/zhangdeman/wrapper"
2024-08-23 12:26:03 +08:00
"gorm.io/gorm"
2024-08-21 21:00:11 +08:00
)
var (
Exec = &execute{}
)
type execute struct {
2024-08-21 21:11:19 +08:00
databaseClientManager abstract.IWrapperClient // 全部数据库管理的实例
2024-08-23 16:50:47 +08:00
baseDao database.BaseDao // 基础dao
2024-08-21 21:11:19 +08:00
}
// SetDatabaseClientManager 设置数据库连接管理实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:06 2024/8/21
func (e *execute) SetDatabaseClientManager(databaseClientManager abstract.IWrapperClient) {
e.databaseClientManager = databaseClientManager
2024-08-21 21:00:11 +08:00
}
// Run 执行
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:48 2024/8/21
func (e *execute) Run(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
2024-08-23 10:59:20 +08:00
if err := e.formatAndValidateInputParam(inputParam); nil != err {
return nil, err
}
2024-08-21 21:00:11 +08:00
if len(inputParam.InputSql) > 0 {
// 基于表达式执行
return e.Express(ctx, inputParam)
}
switch inputParam.SqlType {
case consts.SqlTypeCount:
return e.Count(ctx, inputParam)
case consts.SqlTypeDetail:
return e.Detail(ctx, inputParam)
case consts.SqlTypeList:
return e.List(ctx, inputParam)
case consts.SqlTypeInsert:
return e.Insert(ctx, inputParam)
case consts.SqlTypeUpdate:
return e.Update(ctx, inputParam)
case consts.SqlTypeDelete:
return e.Delete(ctx, inputParam)
default:
return nil, errors.New(inputParam.SqlType + " : sql type is not support")
}
}
// List 列表
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:52 2024/8/21
func (e *execute) List(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
2024-08-23 16:50:47 +08:00
var (
err error
tx *gorm.DB
2024-08-23 17:54:48 +08:00
optionList []define.SetOption
2024-08-23 16:50:47 +08:00
)
if tx, err = e.getTx(ctx, inputParam); nil != err {
return nil, err
}
if optionList, err = e.getOptionList(ctx, inputParam); nil != err {
return nil, err
}
// 动态生成结果解析结构体
st := wrapper.NewDynamic()
for _, columnConfig := range inputParam.ColumnList {
tag := fmt.Sprintf(`gorm:"%v" json:"%v"`, columnConfig.Column, columnConfig.Alias)
2024-08-23 18:23:37 +08:00
column := wrapper.String(columnConfig.Column).SnakeCaseToCamel()
2024-08-23 16:50:47 +08:00
switch columnConfig.Type {
case "int", "int8", "int16", "int32", "int64":
2024-08-23 18:23:37 +08:00
st.AddInt(column, tag, "")
2024-08-23 16:50:47 +08:00
case "uint", "uint8", "uint16", "uint32", "uint64":
2024-08-23 18:23:37 +08:00
st.AddUint(column, tag, "")
2024-08-23 16:50:47 +08:00
case "bool":
2024-08-23 18:23:37 +08:00
st.AddBool(column, tag, "")
2024-08-23 16:50:47 +08:00
case "float":
2024-08-23 18:23:37 +08:00
st.AddBool(column, tag, "")
2024-08-23 16:50:47 +08:00
case "string":
2024-08-23 18:23:37 +08:00
st.AddString(column, tag, "")
2024-08-23 16:50:47 +08:00
case "map":
2024-08-23 18:23:37 +08:00
st.AddMap(column, tag, "")
2024-08-23 16:50:47 +08:00
case "slice":
2024-08-23 18:23:37 +08:00
st.AddSlice(column, tag, "")
2024-08-23 16:50:47 +08:00
}
}
val := st.ToStructDefaultSliceValue()
if err = e.baseDao.List(tx, &val, optionList...); nil != err {
return nil, err
}
return val, nil
2024-08-21 21:00:11 +08:00
}
// Detail 详情
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:52 2024/8/21
func (e *execute) Detail(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
return nil, nil
}
// Insert 插入
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:52 2024/8/21
func (e *execute) Insert(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
return nil, nil
}
// Update 更新
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:52 2024/8/21
func (e *execute) Update(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
return nil, nil
}
// Count 数量
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:52 2024/8/21
func (e *execute) Count(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
return nil, nil
}
// Delete 删除
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:51 2024/8/21
func (e *execute) Delete(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
return nil, nil
}
// Express 基于表达式执行
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:51 2024/8/21
func (e *execute) Express(ctx context.Context, inputParam *define.Api2SqlParam) (any, error) {
2024-08-22 11:46:00 +08:00
if nil == e.databaseClientManager {
return nil, errors.New("database client is nil, please use `SetDatabaseClientManager` set instance")
}
// 格式化 inputParam
2024-08-21 21:00:11 +08:00
return nil, nil
}
2024-08-22 11:46:00 +08:00
// formatAndValidateInputParam 格式化并校验输入参数
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:38 2024/8/22
func (e *execute) formatAndValidateInputParam(inputParam *define.Api2SqlParam) error {
if nil == inputParam {
return errors.New("inputParam is nil")
}
if len(inputParam.DatabaseFlag) == 0 {
return errors.New("databaseFlag is empty")
}
if len(inputParam.Table) == 0 {
return errors.New("table is empty")
}
if len(inputParam.SqlType) == 0 {
return errors.New("sqlType is empty")
}
databaseClient, err := e.databaseClientManager.GetDBClient(inputParam.DatabaseFlag)
if nil != err {
return errors.New(inputParam.DatabaseFlag + " : database client get fail -> " + err.Error())
}
// 尝试获取表结构, api 转 sql 要求必须开启表结构缓存, 否则无法确定相关数据如何解析
if inputParam.TableColumnConfig, err = databaseClient.GetTableFieldList(inputParam.Table); nil != err {
return errors.New(inputParam.DatabaseFlag + " : get table field list fail -> " + err.Error())
}
if len(inputParam.TableColumnConfig) == 0 {
return errors.New(inputParam.DatabaseFlag + " : table field list is empty, please enable `CacheDataTableStructureConfig` or `SetTableColumnConfig`")
}
// 操作字段列表为空
2024-08-22 11:51:02 +08:00
if err = e.validateColumn(inputParam); nil != err {
return err
}
// 验证 force no limit
if inputParam.ForceNoLimit {
inputParam.Limit = 0
inputParam.WithCount = false
}
return nil
}
// validateColumn 验证表字段相关
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:48 2024/8/22
func (e *execute) validateColumn(inputParam *define.Api2SqlParam) error {
2024-08-23 16:50:47 +08:00
if len(inputParam.ColumnList) == 0 && wrapper.ArrayType[string]([]string{
2024-08-22 11:46:00 +08:00
consts.SqlTypeList, consts.SqlTypeDetail,
}).Has(inputParam.SqlType) >= 0 {
2024-08-23 16:50:47 +08:00
return errors.New("column list is empty")
2024-08-22 11:46:00 +08:00
}
// 验证字段是否都正确
tableColumnTable := make(map[string]bool)
for _, itemColumn := range inputParam.TableColumnConfig {
2024-08-23 17:54:48 +08:00
tableColumnTable[itemColumn.Column] = true
2024-08-22 11:46:00 +08:00
}
2024-08-23 16:50:47 +08:00
for _, columnConfig := range inputParam.ColumnList {
if !tableColumnTable[columnConfig.Column] {
return errors.New(columnConfig.Column + " : input column not found in table column list")
2024-08-22 11:46:00 +08:00
}
2024-08-23 16:50:47 +08:00
if len(columnConfig.Alias) == 0 {
columnConfig.Alias = columnConfig.Column
2024-08-22 11:46:00 +08:00
}
}
return nil
}
2024-08-23 12:26:03 +08:00
// getTx 获取数据库连接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:20 2024/8/23
func (e *execute) getTx(ctx context.Context, inputParam *define.Api2SqlParam) (*gorm.DB, error) {
if nil != inputParam.Tx {
return inputParam.Tx, nil
}
if inputParam.ForceMaster || // 强制操作主库
wrapper.ArrayType[string]([]string{consts.SqlTypeDelete, consts.SqlTypeInsert, consts.SqlTypeUpdate}).Has(inputParam.SqlType) >= 0 { // 写操作
return e.databaseClientManager.GetMasterClient(ctx, inputParam.DatabaseFlag)
}
return e.databaseClientManager.GetSlaveClient(ctx, inputParam.DatabaseFlag)
}
2024-08-23 16:50:47 +08:00
// getOptionList 设置where条件
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:31 2024/8/23
2024-08-23 17:54:48 +08:00
func (e *execute) getOptionList(ctx context.Context, inputParam *define.Api2SqlParam) ([]define.SetOption, error) {
optionList := []define.SetOption{
2024-08-23 16:50:47 +08:00
database.WithTable(inputParam.Table),
}
// 设置 limit offset
if !inputParam.ForceNoLimit && inputParam.Limit > 0 {
optionList = append(optionList, database.WithLimit(inputParam.Limit, inputParam.Offset))
}
for _, item := range inputParam.ConditionList {
optionFunc, err := database.WithAnyCondition(item.Column, item.Operate, item.Value)
if nil != err {
return nil, err
}
optionList = append(optionList, optionFunc)
}
return optionList, nil
}