修复代码BUG + 循环引用

This commit is contained in:
白茶清欢 2024-08-23 17:54:48 +08:00
parent f359598109
commit c6b8d29b61
9 changed files with 154 additions and 115 deletions

View File

@ -8,7 +8,7 @@
package abstract package abstract
import ( import (
"git.zhangdeman.cn/zhangdeman/database" "git.zhangdeman.cn/zhangdeman/database/define"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -19,21 +19,21 @@ import (
// Date : 15:06 2023/10/14 // Date : 15:06 2023/10/14
type IDatabase interface { type IDatabase interface {
// Create 创建数据 // Create 创建数据
Create(dbInstance *gorm.DB, data any, optionList ...database.SetOption) error Create(dbInstance *gorm.DB, data any, optionList ...define.SetOption) error
// Update 更新数据 // Update 更新数据
Update(dbInstance *gorm.DB, updateData any, optionFuncList ...database.SetOption) (int64, error) Update(dbInstance *gorm.DB, updateData any, optionFuncList ...define.SetOption) (int64, error)
// UpdateOne 更新一条数据 // UpdateOne 更新一条数据
UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList ...database.SetOption) (int64, error) UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList ...define.SetOption) (int64, error)
// List 查询数据列表 // List 查询数据列表
List(dbInstance *gorm.DB, result any, optionFuncList ...database.SetOption) error List(dbInstance *gorm.DB, result any, optionFuncList ...define.SetOption) error
// Delete 删除数据 // Delete 删除数据
Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...database.SetOption) (int64, error) Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...define.SetOption) (int64, error)
// Detail 数据详情 // Detail 数据详情
Detail(dbInstance *gorm.DB, result any, optionFuncList ...database.SetOption) error Detail(dbInstance *gorm.DB, result any, optionFuncList ...define.SetOption) error
// IsNotFound 错误是否为数据不存在 // IsNotFound 错误是否为数据不存在
IsNotFound(err error) bool IsNotFound(err error) bool
// Count 查询数据数量 // Count 查询数据数量
Count(dbInstance *gorm.DB, optionFuncList ...database.SetOption) (int64, error) Count(dbInstance *gorm.DB, optionFuncList ...define.SetOption) (int64, error)
// Tx 执行事务 // Tx 执行事务
Tx(dbInstance *gorm.DB, txFunc func(dbInstance *gorm.DB) error) error Tx(dbInstance *gorm.DB, txFunc func(dbInstance *gorm.DB) error) error
// Begin 开启事务 // Begin 开启事务

View File

@ -78,7 +78,7 @@ func (e *execute) List(ctx context.Context, inputParam *define.Api2SqlParam) (an
var ( var (
err error err error
tx *gorm.DB tx *gorm.DB
optionList []database.SetOption optionList []define.SetOption
) )
if tx, err = e.getTx(ctx, inputParam); nil != err { if tx, err = e.getTx(ctx, inputParam); nil != err {
return nil, err return nil, err
@ -227,7 +227,7 @@ func (e *execute) validateColumn(inputParam *define.Api2SqlParam) error {
// 验证字段是否都正确 // 验证字段是否都正确
tableColumnTable := make(map[string]bool) tableColumnTable := make(map[string]bool)
for _, itemColumn := range inputParam.TableColumnConfig { for _, itemColumn := range inputParam.TableColumnConfig {
tableColumnTable[itemColumn.ColumnName] = true tableColumnTable[itemColumn.Column] = true
} }
for _, columnConfig := range inputParam.ColumnList { for _, columnConfig := range inputParam.ColumnList {
if !tableColumnTable[columnConfig.Column] { if !tableColumnTable[columnConfig.Column] {
@ -261,8 +261,8 @@ func (e *execute) getTx(ctx context.Context, inputParam *define.Api2SqlParam) (*
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 12:31 2024/8/23 // Date : 12:31 2024/8/23
func (e *execute) getOptionList(ctx context.Context, inputParam *define.Api2SqlParam) ([]database.SetOption, error) { func (e *execute) getOptionList(ctx context.Context, inputParam *define.Api2SqlParam) ([]define.SetOption, error) {
optionList := []database.SetOption{ optionList := []define.SetOption{
database.WithTable(inputParam.Table), database.WithTable(inputParam.Table),
} }
// 设置 limit offset // 设置 limit offset

31
api2sql/execute_test.go Normal file
View File

@ -0,0 +1,31 @@
// Package api2sql ...
//
// Description : api2sql ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-08-23 17:36
package api2sql
import (
"git.zhangdeman.cn/zhangdeman/database"
"git.zhangdeman.cn/zhangdeman/database/define"
"testing"
)
func Test_execute_Run(t *testing.T) {
clientManager := database.NewWrapperClient()
if err := clientManager.AddWithConfig("TEST_DATABASE", nil, &define.Database{
Master: &define.Driver{
DBType: "sqlite3",
Host: "/tmp/gateway.db",
},
Slave: &define.Driver{
DBType: "sqlite3",
Host: "/tmp/gateway.db",
},
}, []string{}); nil != err {
panic(err.Error())
}
Exec.SetDatabaseClientManager(clientManager)
}

23
base.go
View File

@ -7,6 +7,7 @@ package database
import ( import (
"errors" "errors"
"git.zhangdeman.cn/zhangdeman/database/define"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -25,8 +26,8 @@ type BaseDao struct {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 8:06 下午 2021/8/8 // Date : 8:06 下午 2021/8/8
func (b *BaseDao) Create(dbInstance *gorm.DB, data any, optionList ...SetOption) error { func (b *BaseDao) Create(dbInstance *gorm.DB, data any, optionList ...define.SetOption) error {
o := &Option{} o := &define.Option{}
for _, itemFunc := range optionList { for _, itemFunc := range optionList {
itemFunc(o) itemFunc(o)
} }
@ -42,7 +43,7 @@ func (b *BaseDao) Create(dbInstance *gorm.DB, data any, optionList ...SetOption)
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 8:12 下午 2021/8/8 // Date : 8:12 下午 2021/8/8
func (b *BaseDao) Update(dbInstance *gorm.DB, updateData any, optionFuncList ...SetOption) (int64, error) { func (b *BaseDao) Update(dbInstance *gorm.DB, updateData any, optionFuncList ...define.SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
r := dbInstance.Updates(updateData) r := dbInstance.Updates(updateData)
return r.RowsAffected, r.Error return r.RowsAffected, r.Error
@ -53,7 +54,7 @@ func (b *BaseDao) Update(dbInstance *gorm.DB, updateData any, optionFuncList ...
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:05 2024/1/13 // Date : 17:05 2024/1/13
func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList ...SetOption) (int64, error) { func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList ...define.SetOption) (int64, error) {
optionFuncList = append(optionFuncList, WithLimit(1, 0)) optionFuncList = append(optionFuncList, WithLimit(1, 0))
return b.Update(dbInstance, updateData, optionFuncList...) return b.Update(dbInstance, updateData, optionFuncList...)
} }
@ -63,7 +64,7 @@ func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 8:14 下午 2021/8/8 // Date : 8:14 下午 2021/8/8
func (b *BaseDao) List(dbInstance *gorm.DB, result any, optionFuncList ...SetOption) error { func (b *BaseDao) List(dbInstance *gorm.DB, result any, optionFuncList ...define.SetOption) error {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
return dbInstance.Find(result).Error return dbInstance.Find(result).Error
} }
@ -73,7 +74,7 @@ func (b *BaseDao) List(dbInstance *gorm.DB, result any, optionFuncList ...SetOpt
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 11:46 2023/2/9 // Date : 11:46 2023/2/9
func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...SetOption) (int64, error) { func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...define.SetOption) (int64, error) {
dbInstance = dbInstance.Model(dataModel) dbInstance = dbInstance.Model(dataModel)
dbInstance = b.setTxCondition(dbInstance, optionFuncList...).Delete(dataModel) dbInstance = b.setTxCondition(dbInstance, optionFuncList...).Delete(dataModel)
return dbInstance.RowsAffected, dbInstance.Error return dbInstance.RowsAffected, dbInstance.Error
@ -84,7 +85,7 @@ func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...S
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 8:25 下午 2021/8/8 // Date : 8:25 下午 2021/8/8
func (b *BaseDao) Detail(dbInstance *gorm.DB, result any, optionFuncList ...SetOption) error { func (b *BaseDao) Detail(dbInstance *gorm.DB, result any, optionFuncList ...define.SetOption) error {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
return dbInstance.First(result).Error return dbInstance.First(result).Error
} }
@ -118,7 +119,7 @@ func (b *BaseDao) IsNotFound(err error) bool {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 8:25 下午 2021/8/8 // Date : 8:25 下午 2021/8/8
func (b *BaseDao) Count(dbInstance *gorm.DB, optionFuncList ...SetOption) (int64, error) { func (b *BaseDao) Count(dbInstance *gorm.DB, optionFuncList ...define.SetOption) (int64, error) {
dbInstance = b.setTxCondition(dbInstance, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
var cnt int64 var cnt int64
return cnt, dbInstance.Count(&cnt).Error return cnt, dbInstance.Count(&cnt).Error
@ -173,10 +174,10 @@ func (b *BaseDao) Rollback(db *gorm.DB) error {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:38 2022/5/15 // Date : 17:38 2022/5/15
func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm.DB { func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...define.SetOption) *gorm.DB {
// 构建查询条件 // 构建查询条件
o := &Option{} o := &define.Option{}
for _, fn := range optionFuncList { for _, fn := range optionFuncList {
fn(o) fn(o)
} }
@ -256,7 +257,7 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
// or 语句 // or 语句
if nil != o.OR { if nil != o.OR {
for _, itemOr := range o.OR { for _, itemOr := range o.OR {
orOption := &Option{} orOption := &define.Option{}
for _, fn := range itemOr { for _, fn := range itemOr {
fn(orOption) fn(orOption)
} }

View File

@ -43,7 +43,7 @@ type Api2SqlParam struct {
OrderRule string `json:"order_rule"` // 排序规则, Asc / Desc OrderRule string `json:"order_rule"` // 排序规则, Asc / Desc
WithCount bool `json:"with_count"` // 是否返回数据总量, 仅 sqlType = list 生效 WithCount bool `json:"with_count"` // 是否返回数据总量, 仅 sqlType = list 生效
ConditionList []SqlCondition `json:"value_list"` // 字段列表 ConditionList []SqlCondition `json:"value_list"` // 字段列表
TableColumnConfig []*ColumnInfo `json:"table_column_config"` // 表字段配置 TableColumnConfig []*ColumnConfig `json:"table_column_config"` // 表字段配置
Tx *gorm.DB `json:"-"` // 前后已有的数据库连接, 直接复用 Tx *gorm.DB `json:"-"` // 前后已有的数据库连接, 直接复用
} }

39
define/sql_option.go Normal file
View File

@ -0,0 +1,39 @@
// Package define ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-08-23 17:46
package define
// SetOption 设置选项
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:46 2022/5/15
type SetOption func(o *Option)
// Option 扩展选项
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:05 下午 2021/8/8
type Option struct {
Model any `json:"-"` // 操作model
Table string `json:"table"` // 查询的数据表
Limit int `json:"limit"` // 限制数量
Offset int `json:"offset"` // 偏移量
In map[string]any `json:"in"` // in语句
NotIn map[string]any `json:"not_in"` // not in语句
Where map[string]any `json:"where"` // where 条件
Between map[string][2]any `json:"between"` // between 条件
NotBetween map[string][2]any `json:"not_between"` // not between 条件
Start map[string]any `json:"start"` // >= 条件
End map[string]any `json:"end"` // < 条件
Like map[string]string `json:"like"` // like 语句
NotLike map[string]string `json:"not_like"` // not like 语句
NotEqual map[string]any `json:"not_equal"` // != 语句
Order []string `json:"order"` // 排序规则
OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
}

142
option.go
View File

@ -12,50 +12,20 @@ import (
"errors" "errors"
"fmt" "fmt"
"git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/database/define"
"git.zhangdeman.cn/zhangdeman/op_type" "git.zhangdeman.cn/zhangdeman/op_type"
"git.zhangdeman.cn/zhangdeman/serialize" "git.zhangdeman.cn/zhangdeman/serialize"
"reflect" "reflect"
"strings" "strings"
) )
// SetOption 设置选项
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:46 2022/5/15
type SetOption func(o *Option)
// Option 扩展选项
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:05 下午 2021/8/8
type Option struct {
Model any `json:"-"` // 操作model
Table string `json:"table"` // 查询的数据表
Limit int `json:"limit"` // 限制数量
Offset int `json:"offset"` // 偏移量
In map[string]any `json:"in"` // in语句
NotIn map[string]any `json:"not_in"` // not in语句
Where map[string]any `json:"where"` // where 条件
Between map[string][2]any `json:"between"` // between 条件
NotBetween map[string][2]any `json:"not_between"` // not between 条件
Start map[string]any `json:"start"` // >= 条件
End map[string]any `json:"end"` // < 条件
Like map[string]string `json:"like"` // like 语句
NotLike map[string]string `json:"not_like"` // not like 语句
NotEqual map[string]any `json:"not_equal"` // != 语句
Order []string `json:"order"` // 排序规则
OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
}
// WithModel ... // WithModel ...
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:18 2024/1/15 // Date : 15:18 2024/1/15
func WithModel(model any) SetOption { func WithModel(model any) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
o.Model = model o.Model = model
} }
} }
@ -65,8 +35,8 @@ func WithModel(model any) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 20:56 2023/3/22 // Date : 20:56 2023/3/22
func WithTable(tableName string) SetOption { func WithTable(tableName string) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
o.Table = tableName o.Table = tableName
} }
} }
@ -76,8 +46,8 @@ func WithTable(tableName string) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 12:17 2022/5/15 // Date : 12:17 2022/5/15
func WithWhere[T op_type.BaseType](where map[string]T) SetOption { func WithWhere[T op_type.BaseType](where map[string]T) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.Where { if nil == o.Where {
o.Where = make(map[string]any) o.Where = make(map[string]any)
} }
@ -92,8 +62,8 @@ func WithWhere[T op_type.BaseType](where map[string]T) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 12:00 2022/5/15 // Date : 12:00 2022/5/15
func WithLimit[T op_type.Int](limit T, offset T) SetOption { func WithLimit[T op_type.Int](limit T, offset T) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
o.Limit = int(limit) o.Limit = int(limit)
o.Offset = int(offset) o.Offset = int(offset)
} }
@ -104,8 +74,8 @@ func WithLimit[T op_type.Int](limit T, offset T) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 21:42 2023/10/14 // Date : 21:42 2023/10/14
func WithLimitByPageAndSize[T op_type.Int](page T, size T) SetOption { func WithLimitByPageAndSize[T op_type.Int](page T, size T) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if size > 0 { if size > 0 {
o.Limit = int(size) o.Limit = int(size)
} }
@ -121,8 +91,8 @@ func WithLimitByPageAndSize[T op_type.Int](page T, size T) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 12:23 2022/5/15 // Date : 12:23 2022/5/15
func WithIn[T op_type.Array](field string, value T) SetOption { func WithIn[T op_type.Array](field string, value T) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == value { if nil == value {
return return
} }
@ -141,8 +111,8 @@ func WithIn[T op_type.Array](field string, value T) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 12:24 2022/5/15 // Date : 12:24 2022/5/15
func WithBatchIn[T op_type.Array](batchIn map[string]T) SetOption { func WithBatchIn[T op_type.Array](batchIn map[string]T) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.In { if nil == o.In {
o.In = make(map[string]any) o.In = make(map[string]any)
} }
@ -157,8 +127,8 @@ func WithBatchIn[T op_type.Array](batchIn map[string]T) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:18 2022/5/15 // Date : 16:18 2022/5/15
func WithNotIn[T op_type.Array](field string, value T) SetOption { func WithNotIn[T op_type.Array](field string, value T) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.NotIn { if nil == o.NotIn {
o.NotIn = make(map[string]any) o.NotIn = make(map[string]any)
} }
@ -174,8 +144,8 @@ func WithNotIn[T op_type.Array](field string, value T) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:23 2022/5/15 // Date : 16:23 2022/5/15
func WithBatchNotIn[T op_type.Array](data map[string]T) SetOption { func WithBatchNotIn[T op_type.Array](data map[string]T) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.NotIn { if nil == o.NotIn {
o.NotIn = make(map[string]any) o.NotIn = make(map[string]any)
} }
@ -193,8 +163,8 @@ func WithBatchNotIn[T op_type.Array](data map[string]T) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:01 2022/5/15 // Date : 17:01 2022/5/15
func WithStart(field string, value any) SetOption { func WithStart(field string, value any) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.Start { if nil == o.Start {
o.Start = make(map[string]any) o.Start = make(map[string]any)
} }
@ -207,8 +177,8 @@ func WithStart(field string, value any) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:03 2022/5/15 // Date : 17:03 2022/5/15
func WithBatchStart(data map[string]any) SetOption { func WithBatchStart(data map[string]any) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.Start { if nil == o.Start {
o.Start = make(map[string]any) o.Start = make(map[string]any)
} }
@ -223,8 +193,8 @@ func WithBatchStart(data map[string]any) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:07 2022/5/15 // Date : 17:07 2022/5/15
func WithEnd(field string, value any) SetOption { func WithEnd(field string, value any) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.End { if nil == o.End {
o.End = make(map[string]any) o.End = make(map[string]any)
} }
@ -237,8 +207,8 @@ func WithEnd(field string, value any) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:10 2022/5/15 // Date : 17:10 2022/5/15
func WithBatchEnd(data map[string]any) SetOption { func WithBatchEnd(data map[string]any) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.End { if nil == o.End {
o.End = make(map[string]any) o.End = make(map[string]any)
} }
@ -253,8 +223,8 @@ func WithBatchEnd(data map[string]any) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:16 2022/5/15 // Date : 17:16 2022/5/15
func WithLike(field string, value string) SetOption { func WithLike(field string, value string) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.Like { if nil == o.Like {
o.Like = make(map[string]string) o.Like = make(map[string]string)
} }
@ -270,8 +240,8 @@ func WithLike(field string, value string) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:19 2022/5/15 // Date : 17:19 2022/5/15
func WithBatchLike(data map[string]string) SetOption { func WithBatchLike(data map[string]string) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.Like { if nil == o.Like {
o.Like = make(map[string]string) o.Like = make(map[string]string)
} }
@ -289,8 +259,8 @@ func WithBatchLike(data map[string]string) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:50 2022/5/15 // Date : 17:50 2022/5/15
func WithNotLike(field string, value string) SetOption { func WithNotLike(field string, value string) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.NotLike { if nil == o.NotLike {
o.NotLike = make(map[string]string) o.NotLike = make(map[string]string)
} }
@ -306,8 +276,8 @@ func WithNotLike(field string, value string) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:52 2022/5/15 // Date : 17:52 2022/5/15
func WithBatchNotLike(data map[string]string) SetOption { func WithBatchNotLike(data map[string]string) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.NotLike { if nil == o.NotLike {
o.NotLike = make(map[string]string) o.NotLike = make(map[string]string)
} }
@ -325,8 +295,8 @@ func WithBatchNotLike(data map[string]string) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:31 2022/5/15 // Date : 17:31 2022/5/15
func WithNotEqual[T op_type.BaseType](field string, value T) SetOption { func WithNotEqual[T op_type.BaseType](field string, value T) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.NotEqual { if nil == o.NotEqual {
o.NotEqual = make(map[string]any) o.NotEqual = make(map[string]any)
} }
@ -339,8 +309,8 @@ func WithNotEqual[T op_type.BaseType](field string, value T) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:33 2022/5/15 // Date : 17:33 2022/5/15
func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption { func WithBatchNotEqual[T op_type.BaseType](data map[string]T) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.NotEqual { if nil == o.NotEqual {
o.NotEqual = make(map[string]any) o.NotEqual = make(map[string]any)
} }
@ -355,10 +325,10 @@ func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 20:03 2022/7/23 // Date : 20:03 2022/7/23
func WithOR(orConditionList ...SetOption) SetOption { func WithOR(orConditionList ...define.SetOption) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.OR { if nil == o.OR {
o.OR = make([][]SetOption, 0) o.OR = make([][]define.SetOption, 0)
} }
o.OR = append(o.OR, orConditionList) o.OR = append(o.OR, orConditionList)
} }
@ -369,8 +339,8 @@ func WithOR(orConditionList ...SetOption) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 20:15 2022/10/20 // Date : 20:15 2022/10/20
func WithOrder(orderRuleList ...string) SetOption { func WithOrder(orderRuleList ...string) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
o.Order = orderRuleList o.Order = orderRuleList
} }
} }
@ -380,8 +350,8 @@ func WithOrder(orderRuleList ...string) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:09 2023/4/3 // Date : 15:09 2023/4/3
func WithOrderDesc(field string) SetOption { func WithOrderDesc(field string) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.Order { if nil == o.Order {
o.Order = make([]string, 0) o.Order = make([]string, 0)
} }
@ -394,8 +364,8 @@ func WithOrderDesc(field string) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:09 2023/4/3 // Date : 15:09 2023/4/3
func WithOrderAsc(field string) SetOption { func WithOrderAsc(field string) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.Order { if nil == o.Order {
o.Order = make([]string, 0) o.Order = make([]string, 0)
} }
@ -408,8 +378,8 @@ func WithOrderAsc(field string) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:46 2024/8/9 // Date : 17:46 2024/8/9
func newOption(setOptionList ...SetOption) *Option { func newOption(setOptionList ...define.SetOption) *define.Option {
o := &Option{} o := &define.Option{}
for _, item := range setOptionList { for _, item := range setOptionList {
item(o) item(o)
} }
@ -421,7 +391,7 @@ func newOption(setOptionList ...SetOption) *Option {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:46 2024/8/9 // Date : 17:46 2024/8/9
func optionToSql(o *Option) (sqlBuildResult string, bindValue []any) { func optionToSql(o *define.Option) (sqlBuildResult string, bindValue []any) {
bindValue = make([]any, 0) bindValue = make([]any, 0)
sqlBuildResultBlockList := make([]string, 0) sqlBuildResultBlockList := make([]string, 0)
// 设置where条件 // 设置where条件
@ -531,8 +501,8 @@ func parseInSql(fieldValue any) (string, []any) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:52 2024/8/23 // Date : 15:52 2024/8/23
func WithBetween(field string, left any, right any) SetOption { func WithBetween(field string, left any, right any) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.Between { if nil == o.Between {
o.Between = map[string][2]any{} o.Between = map[string][2]any{}
} }
@ -545,8 +515,8 @@ func WithBetween(field string, left any, right any) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:52 2024/8/23 // Date : 15:52 2024/8/23
func WithNotBetween(field string, left any, right any) SetOption { func WithNotBetween(field string, left any, right any) define.SetOption {
return func(o *Option) { return func(o *define.Option) {
if nil == o.NotBetween { if nil == o.NotBetween {
o.NotBetween = map[string][2]any{} o.NotBetween = map[string][2]any{}
} }
@ -559,7 +529,7 @@ func WithNotBetween(field string, left any, right any) SetOption {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:48 2024/8/23 // Date : 14:48 2024/8/23
func WithAnyCondition(column string, operate string, value any) (SetOption, error) { func WithAnyCondition(column string, operate string, value any) (define.SetOption, error) {
if nil == value { if nil == value {
return nil, errors.New("value is nil") return nil, errors.New("value is nil")
} }

View File

@ -9,14 +9,12 @@ package database
import ( import (
"fmt" "fmt"
"git.zhangdeman.cn/zhangdeman/database/define"
"testing" "testing"
) )
func Test_optionToSql(t *testing.T) { func Test_optionToSql(t *testing.T) {
type args struct { o := &define.Option{
o *Option
}
o := &Option{
In: nil, In: nil,
NotIn: nil, NotIn: nil,
Where: map[string]any{ Where: map[string]any{

View File

@ -80,7 +80,7 @@ func (c *wrapperClient) AddWithConfig(flag string, logInstance *zap.Logger, data
Cfg: define.Driver{}, Cfg: define.Driver{},
} }
if err := dbClient.Init(databaseConfig); nil != err { if err := dbClient.Init(databaseConfig, nil); nil != err {
return err return err
} }
c.lock.Lock() c.lock.Lock()