From ce2f962784054281cb9697eed33aa34aa1680705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 9 Aug 2024 17:17:20 +0800 Subject: [PATCH 1/3] interface -> any --- abstract/base.go | 12 ++++----- base.go | 18 ++++++------- option.go | 64 +++++++++++++++++++++++----------------------- sql2go/dao_tpl.tpl | 4 +-- sql2go/define.go | 4 +-- sql2go/go_test.go | 4 +-- system.go | 4 +-- 7 files changed, 55 insertions(+), 55 deletions(-) diff --git a/abstract/base.go b/abstract/base.go index 52733a5..a14066d 100644 --- a/abstract/base.go +++ b/abstract/base.go @@ -19,17 +19,17 @@ import ( // Date : 15:06 2023/10/14 type Base interface { // Create 创建数据 - Create(dbInstance *gorm.DB, data interface{}, optionList ...database.SetOption) error + Create(dbInstance *gorm.DB, data any, optionList ...database.SetOption) error // Update 更新数据 - Update(dbInstance *gorm.DB, updateData interface{}, optionFuncList ...database.SetOption) (int64, error) + Update(dbInstance *gorm.DB, updateData any, optionFuncList ...database.SetOption) (int64, error) // UpdateOne 更新一条数据 - UpdateOne(dbInstance *gorm.DB, updateData interface{}, optionFuncList ...database.SetOption) (int64, error) + UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList ...database.SetOption) (int64, error) // List 查询数据列表 - List(dbInstance *gorm.DB, result interface{}, optionFuncList ...database.SetOption) error + List(dbInstance *gorm.DB, result any, optionFuncList ...database.SetOption) error // Delete 删除数据 - Delete(dbInstance *gorm.DB, dataModel interface{}, optionFuncList ...database.SetOption) (int64, error) + Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...database.SetOption) (int64, error) // Detail 数据详情 - Detail(dbInstance *gorm.DB, result interface{}, optionFuncList ...database.SetOption) error + Detail(dbInstance *gorm.DB, result any, optionFuncList ...database.SetOption) error // IsNotFound 错误是否为数据不存在 IsNotFound(err error) bool // Count 查询数据数量 diff --git a/base.go b/base.go index c520734..b9cff80 100644 --- a/base.go +++ b/base.go @@ -25,7 +25,7 @@ type BaseDao struct { // Author : go_developer@163.com<白茶清欢> // // Date : 8:06 下午 2021/8/8 -func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}, optionList ...SetOption) error { +func (b *BaseDao) Create(dbInstance *gorm.DB, data any, optionList ...SetOption) error { o := &Option{} for _, itemFunc := range optionList { itemFunc(o) @@ -42,7 +42,7 @@ func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}, optionList ...Se // Author : go_developer@163.com<白茶清欢> // // Date : 8:12 下午 2021/8/8 -func (b *BaseDao) Update(dbInstance *gorm.DB, updateData interface{}, optionFuncList ...SetOption) (int64, error) { +func (b *BaseDao) Update(dbInstance *gorm.DB, updateData any, optionFuncList ...SetOption) (int64, error) { dbInstance = b.setTxCondition(dbInstance, optionFuncList...) r := dbInstance.Updates(updateData) return r.RowsAffected, r.Error @@ -53,7 +53,7 @@ func (b *BaseDao) Update(dbInstance *gorm.DB, updateData interface{}, optionFunc // Author : go_developer@163.com<白茶清欢> // // Date : 17:05 2024/1/13 -func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData interface{}, optionFuncList ...SetOption) (int64, error) { +func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList ...SetOption) (int64, error) { optionFuncList = append(optionFuncList, WithLimit(1, 0)) return b.Update(dbInstance, updateData, optionFuncList...) } @@ -63,7 +63,7 @@ func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData interface{}, optionF // Author : go_developer@163.com<白茶清欢> // // Date : 8:14 下午 2021/8/8 -func (b *BaseDao) List(dbInstance *gorm.DB, result interface{}, optionFuncList ...SetOption) error { +func (b *BaseDao) List(dbInstance *gorm.DB, result any, optionFuncList ...SetOption) error { dbInstance = b.setTxCondition(dbInstance, optionFuncList...) return dbInstance.Find(result).Error } @@ -73,7 +73,7 @@ func (b *BaseDao) List(dbInstance *gorm.DB, result interface{}, optionFuncList . // Author : go_developer@163.com<白茶清欢> // // Date : 11:46 2023/2/9 -func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel interface{}, optionFuncList ...SetOption) (int64, error) { +func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...SetOption) (int64, error) { dbInstance = dbInstance.Model(dataModel) dbInstance = b.setTxCondition(dbInstance, optionFuncList...).Delete(dataModel) return dbInstance.RowsAffected, dbInstance.Error @@ -84,7 +84,7 @@ func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel interface{}, optionFuncL // Author : go_developer@163.com<白茶清欢> // // Date : 8:25 下午 2021/8/8 -func (b *BaseDao) Detail(dbInstance *gorm.DB, result interface{}, optionFuncList ...SetOption) error { +func (b *BaseDao) Detail(dbInstance *gorm.DB, result any, optionFuncList ...SetOption) error { dbInstance = b.setTxCondition(dbInstance, optionFuncList...) return dbInstance.First(result).Error } @@ -94,12 +94,12 @@ func (b *BaseDao) Detail(dbInstance *gorm.DB, result interface{}, optionFuncList // Author : go_developer@163.com<白茶清欢> // // Date : 14:14 2023/10/15 -func (b *BaseDao) DetailByPrimaryID(dbInstance *gorm.DB, result interface{}, primaryID interface{}, primaryKey ...string) error { +func (b *BaseDao) DetailByPrimaryID(dbInstance *gorm.DB, result any, primaryID any, primaryKey ...string) error { primaryKeyField := "id" if len(primaryKey) > 0 { primaryKeyField = primaryKey[0] } - return b.Detail(dbInstance, result, WithWhere(map[string]interface{}{ + return b.Detail(dbInstance, result, WithWhere(map[string]any{ primaryKeyField: primaryID, })) } @@ -253,7 +253,7 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm // or 语句 if nil != o.OR { expression := "" - valList := make([]interface{}, 0) + valList := make([]any, 0) for _, o := range o.OR { if len(expression) > 0 { expression = expression + " OR " diff --git a/option.go b/option.go index 3008b24..31a4319 100644 --- a/option.go +++ b/option.go @@ -24,8 +24,8 @@ type SetOption func(o *Option) // // Date : 20:07 2022/7/23 type ORCondition struct { - Express string `json:"express"` // 表达式 - Value interface{} `json:"value"` // 绑定值 + Express string `json:"express"` // 表达式 + Value any `json:"value"` // 绑定值 } // Option 扩展选项 @@ -34,20 +34,20 @@ type ORCondition struct { // // Date : 8:05 下午 2021/8/8 type Option struct { - Model interface{} `json:"-"` // 操作model - Table string `json:"table"` // 查询的数据表 - Limit int `json:"limit"` // 限制数量 - Offset int `json:"offset"` // 偏移量 - In map[string]interface{} `json:"in"` // in语句 - NotIn map[string]interface{} `json:"not_in"` // not in语句 - Where map[string]interface{} `json:"where"` // where 条件 - Start map[string]interface{} `json:"start"` // >= 条件 - End map[string]interface{} `json:"end"` // < 条件 - Like map[string]string `json:"like"` // like 语句 - NotLike map[string]string `json:"not_like"` // not like 语句 - NotEqual map[string]interface{} `json:"not_equal"` // != 语句 - Order []string `json:"order"` // 排序规则 - OR []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系 + 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 条件 + 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 []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系 } // WithModel ... @@ -55,7 +55,7 @@ type Option struct { // Author : go_developer@163.com<白茶清欢> // // Date : 15:18 2024/1/15 -func WithModel(model interface{}) SetOption { +func WithModel(model any) SetOption { return func(o *Option) { o.Model = model } @@ -80,7 +80,7 @@ func WithTable(tableName string) SetOption { func WithWhere[T op_type.BaseType](where map[string]T) SetOption { return func(o *Option) { if nil == o.Where { - o.Where = make(map[string]interface{}) + o.Where = make(map[string]any) } for field, value := range where { o.Where[field] = value @@ -131,7 +131,7 @@ func WithIn[T op_type.Array](field string, value T) SetOption { return } if nil == o.In { - o.In = make(map[string]interface{}) + o.In = make(map[string]any) } o.In[field] = value } @@ -145,7 +145,7 @@ func WithIn[T op_type.Array](field string, value T) SetOption { func WithBatchIn[T op_type.Array](batchIn map[string]T) SetOption { return func(o *Option) { if nil == o.In { - o.In = make(map[string]interface{}) + o.In = make(map[string]any) } for field, value := range batchIn { WithIn(field, value) @@ -161,7 +161,7 @@ func WithBatchIn[T op_type.Array](batchIn map[string]T) SetOption { func WithNotIn[T op_type.Array](field string, value T) SetOption { return func(o *Option) { if nil == o.NotIn { - o.NotIn = make(map[string]interface{}) + o.NotIn = make(map[string]any) } if value == nil || len(value) == 0 { return @@ -178,7 +178,7 @@ func WithNotIn[T op_type.Array](field string, value T) SetOption { func WithBatchNotIn[T op_type.Array](data map[string]T) SetOption { return func(o *Option) { if nil == o.NotIn { - o.NotIn = make(map[string]interface{}) + o.NotIn = make(map[string]any) } for field, value := range data { if value == nil || len(value) == 0 { @@ -194,10 +194,10 @@ func WithBatchNotIn[T op_type.Array](data map[string]T) SetOption { // Author : go_developer@163.com<白茶清欢> // // Date : 17:01 2022/5/15 -func WithStart(field string, value interface{}) SetOption { +func WithStart(field string, value any) SetOption { return func(o *Option) { if nil == o.Start { - o.Start = make(map[string]interface{}) + o.Start = make(map[string]any) } o.Start[field] = value } @@ -208,10 +208,10 @@ func WithStart(field string, value interface{}) SetOption { // Author : go_developer@163.com<白茶清欢> // // Date : 17:03 2022/5/15 -func WithBatchStart(data map[string]interface{}) SetOption { +func WithBatchStart(data map[string]any) SetOption { return func(o *Option) { if nil == o.Start { - o.Start = make(map[string]interface{}) + o.Start = make(map[string]any) } for field, value := range data { o.Start[field] = value @@ -224,10 +224,10 @@ func WithBatchStart(data map[string]interface{}) SetOption { // Author : go_developer@163.com<白茶清欢> // // Date : 17:07 2022/5/15 -func WithEnd(field string, value interface{}) SetOption { +func WithEnd(field string, value any) SetOption { return func(o *Option) { if nil == o.End { - o.End = make(map[string]interface{}) + o.End = make(map[string]any) } o.End[field] = value } @@ -238,10 +238,10 @@ func WithEnd(field string, value interface{}) SetOption { // Author : go_developer@163.com<白茶清欢> // // Date : 17:10 2022/5/15 -func WithBatchEnd(data map[string]interface{}) SetOption { +func WithBatchEnd(data map[string]any) SetOption { return func(o *Option) { if nil == o.End { - o.End = make(map[string]interface{}) + o.End = make(map[string]any) } for field, value := range data { o.End[field] = value @@ -329,7 +329,7 @@ func WithBatchNotLike(data map[string]string) SetOption { func WithNotEqual[T op_type.BaseType](field string, value T) SetOption { return func(o *Option) { if nil == o.NotEqual { - o.NotEqual = make(map[string]interface{}) + o.NotEqual = make(map[string]any) } o.NotEqual[field] = value } @@ -343,7 +343,7 @@ func WithNotEqual[T op_type.BaseType](field string, value T) SetOption { func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption { return func(o *Option) { if nil == o.NotEqual { - o.NotEqual = make(map[string]interface{}) + o.NotEqual = make(map[string]any) } for field, value := range data { o.NotEqual[field] = value diff --git a/sql2go/dao_tpl.tpl b/sql2go/dao_tpl.tpl index be7f268..ede3a26 100644 --- a/sql2go/dao_tpl.tpl +++ b/sql2go/dao_tpl.tpl @@ -36,9 +36,9 @@ func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetDetailBy{PRIMARY_KEY}(dbInstance *go } // GetList 获取数据列表 -func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetList(dbInstance *gorm.DB, condition map[string]interface{}, limit int, offset int) ([]{DATA_STRUCT_NAME}, error) { +func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetList(dbInstance *gorm.DB, condition map[string]any, limit int, offset int) ([]{DATA_STRUCT_NAME}, error) { if nil == condition { - condition = make(map[string]interface{}) + condition = make(map[string]any) } var ( err error diff --git a/sql2go/define.go b/sql2go/define.go index 6c08512..b89a632 100644 --- a/sql2go/define.go +++ b/sql2go/define.go @@ -90,9 +90,9 @@ func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetDetailBy{PRIMARY_KEY}(dbInstance *go } // GetList 获取数据列表 -func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetList(dbInstance *gorm.DB, condition map[string]interface{}, limit int, offset int) ([]{DATA_STRUCT_NAME}, error) { +func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetList(dbInstance *gorm.DB, condition map[string]any, limit int, offset int) ([]{DATA_STRUCT_NAME}, error) { if nil == condition { - condition = make(map[string]interface{}) + condition = make(map[string]any) } var ( err error diff --git a/sql2go/go_test.go b/sql2go/go_test.go index cd06500..7e8662e 100644 --- a/sql2go/go_test.go +++ b/sql2go/go_test.go @@ -47,9 +47,9 @@ func (ad *AppDao) GetDetailByID(dbInstance *gorm.DB, ID int64) (*App, error) { } // GetList 获取数据列表 -func (ad *AppDao) GetList(dbInstance *gorm.DB, condition map[string]interface{}, limit int, offset int) ([]App, error) { +func (ad *AppDao) GetList(dbInstance *gorm.DB, condition map[string]any, limit int, offset int) ([]App, error) { if nil == condition { - condition = make(map[string]interface{}) + condition = make(map[string]any) } var ( err error diff --git a/system.go b/system.go index 790c4a5..ae9cecd 100644 --- a/system.go +++ b/system.go @@ -85,7 +85,7 @@ func (sd *SystemDao) GetTableList(dbInstance *gorm.DB) ([]string, error) { func (sd *SystemDao) GetCreateTableSQL(dbInstance *gorm.DB, table string) (string, error) { var ( err error - result map[string]interface{} + result map[string]any ) if err = dbInstance.Raw("SHOW CREATE TABLE `" + table + "`").Scan(&result).Error; nil != err { @@ -152,6 +152,6 @@ func (sd *SystemDao) CreateTable(dbInstance *gorm.DB, createTableSql string) err // Author : go_developer@163.com<白茶清欢> // // Date : 16:35 2023/9/30 -func (sd *SystemDao) ExecuteSql(dbInstance *gorm.DB, anySql string, value ...interface{}) error { +func (sd *SystemDao) ExecuteSql(dbInstance *gorm.DB, anySql string, value ...any) error { return dbInstance.Exec(anySql, value...).Error } -- 2.36.6 From 88f9e831d8745339a11d6a7c54968a510f2ec9be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 9 Aug 2024 17:24:47 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=AE=A1=E5=88=92=E5=8D=87=E7=BA=A7or?= =?UTF-8?q?=E8=AF=AD=E5=8F=A5=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.go | 22 ++++++---------------- option.go | 18 ++++-------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/base.go b/base.go index b9cff80..2bc37b8 100644 --- a/base.go +++ b/base.go @@ -192,11 +192,6 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm tx = tx.Model(o.Model) } - // 设置where条件 - if nil != o.Where && len(o.Where) > 0 { - tx = tx.Where(o.Where) - } - // 设置 limit offset if o.Limit > 0 { tx = tx.Limit(o.Limit) @@ -205,6 +200,11 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm } } + // 设置where条件 + if nil != o.Where && len(o.Where) > 0 { + tx = tx.Where(o.Where) + } + // in 语句 if nil != o.In { tx = tx.Where(o.In) @@ -251,17 +251,7 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm } // or 语句 - if nil != o.OR { - expression := "" - valList := make([]any, 0) - for _, o := range o.OR { - if len(expression) > 0 { - expression = expression + " OR " - } - expression = expression + " " + o.Express - valList = append(valList, o.Value) - } - tx = tx.Where(expression, valList...) + if len(o.OR) > 0 { } return tx } diff --git a/option.go b/option.go index 31a4319..293e6dc 100644 --- a/option.go +++ b/option.go @@ -18,16 +18,6 @@ import ( // Date : 11:46 2022/5/15 type SetOption func(o *Option) -// ORCondition OR 条件 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 20:07 2022/7/23 -type ORCondition struct { - Express string `json:"express"` // 表达式 - Value any `json:"value"` // 绑定值 -} - // Option 扩展选项 // // Author : go_developer@163.com<白茶清欢> @@ -47,7 +37,7 @@ type Option struct { NotLike map[string]string `json:"not_like"` // not like 语句 NotEqual map[string]any `json:"not_equal"` // != 语句 Order []string `json:"order"` // 排序规则 - OR []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系 + OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系 } // WithModel ... @@ -356,12 +346,12 @@ func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption { // Author : go_developer@163.com<白茶清欢> // // Date : 20:03 2022/7/23 -func WithOR(orConditionList ...*ORCondition) SetOption { +func WithOR(orConditionList ...SetOption) SetOption { return func(o *Option) { if nil == o.OR { - o.OR = make([]*ORCondition, 0) + o.OR = make([][]SetOption, 0) } - o.OR = append(o.OR, orConditionList...) + o.OR = append(o.OR, orConditionList) } } -- 2.36.6 From 7126765fd57d7d6e4289194c07d33d1eba42d4c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 9 Aug 2024 18:26:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=8D=87=E7=BA=A7sql=E6=9E=84=E5=BB=BA,=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81or=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.go | 64 +++++++-------------------------- option.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ option_test.go | 41 +++++++++++++++++++++ 3 files changed, 149 insertions(+), 52 deletions(-) create mode 100644 option_test.go diff --git a/base.go b/base.go index 2bc37b8..0b20801 100644 --- a/base.go +++ b/base.go @@ -199,59 +199,19 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm tx = tx.Offset(o.Offset) } } - - // 设置where条件 - if nil != o.Where && len(o.Where) > 0 { - tx = tx.Where(o.Where) - } - - // in 语句 - if nil != o.In { - tx = tx.Where(o.In) - } - - // not in 语句 - if nil != o.NotIn { - for field, value := range o.NotIn { - tx = tx.Where(field+" NOT IN ? ", value) + sqlBlockList := make([]string, 0) + bindValueList := make([]any, 0) + sql, bindVal := optionToSql(o) + tx.Or(sql, bindVal...) + sqlBlockList = append(sqlBlockList, sql) + bindValueList = append(bindValueList, bindVal) + for _, itemOrFuncList := range o.OR { + orOption := &Option{} + for _, fn := range itemOrFuncList { + fn(orOption) } - } - - // like 语句 - if nil != o.Like { - for field, value := range o.Like { - tx = tx.Where(field+" LIKE ? ", "%"+value+"%") - } - } - - // NOT LIKE 语句 - if nil != o.NotLike { - for field, value := range o.NotLike { - tx = tx.Where(field+" NOT LIKE ? ", "%"+value+"%") - } - } - - // >= - if nil != o.Start { - for field, value := range o.Start { - tx = tx.Where(field+" >= ?", value) - } - } - - // < - if nil != o.End { - for field, value := range o.End { - tx = tx.Where(field+" < ?", value) - } - } - - // 排序 - for _, orderRule := range o.Order { - tx = tx.Order(orderRule) - } - - // or 语句 - if len(o.OR) > 0 { + orSql, orBindVal := optionToSql(orOption) + tx.Or(orSql, orBindVal...) } return tx } diff --git a/option.go b/option.go index 293e6dc..e0bd1ee 100644 --- a/option.go +++ b/option.go @@ -8,7 +8,10 @@ package database import ( + "encoding/json" "git.zhangdeman.cn/zhangdeman/op_type" + "reflect" + "strings" ) // SetOption 设置选项 @@ -393,3 +396,96 @@ func WithOrderAsc(field string) SetOption { o.Order = append(o.Order, field+" asc") } } + +// newOption 生成新的option +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:46 2024/8/9 +func newOption(setOptionList ...SetOption) *Option { + o := &Option{} + for _, item := range setOptionList { + item(o) + } + return o +} + +// optionToSql 基于 option 配置生成sql +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:46 2024/8/9 +func optionToSql(o *Option) (sqlBuildResult string, bindValue []any) { + bindValue = make([]any, 0) + sqlBuildResultBlockList := make([]string, 0) + // 设置where条件 + for fieldName, fieldValue := range o.Where { + if nil == fieldValue { + continue + } + if reflect.TypeOf(fieldValue).Kind() == reflect.Slice { + // 传入数组, in语句 + sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` in (?)") + byteData, _ := json.Marshal(fieldValue) + bindValue = append(bindValue, strings.TrimRight(strings.TrimLeft(string(byteData), "["), "]")) + } else { + sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` = ?") + bindValue = append(bindValue, fieldValue) + } + } + // 设置不等于 + for fieldName, fieldValue := range o.NotEqual { + sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` != ?") + bindValue = append(bindValue, fieldValue) + } + + // in 语句 + // 传入数组, in语句 + for fieldName, fieldValue := range o.In { + sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` in (?)") + byteData, _ := json.Marshal(fieldValue) + bindValue = append(bindValue, strings.TrimRight(strings.TrimLeft(string(byteData), "["), "]")) + } + + // not in 语句 + for fieldName, fieldValue := range o.NotIn { + sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` NOT IN (?)") + byteData, _ := json.Marshal(fieldValue) + bindValue = append(bindValue, strings.TrimRight(strings.TrimLeft(string(byteData), "["), "]")) + } + + // like 语句 + for fieldName, fieldValue := range o.Like { + if len(fieldValue) == 0 { + continue + } + sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` LIKE \"%?%\"") + bindValue = append(bindValue, fieldValue) + } + + // NOT LIKE 语句 + for fieldName, fieldValue := range o.NotLike { + if len(fieldValue) == 0 { + continue + } + sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` NOT LIKE \"%?%\"") + bindValue = append(bindValue, fieldValue) + } + + // >= + for fieldName, fieldValue := range o.Start { + sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` >= ?") + bindValue = append(bindValue, fieldValue) + } + + // < + for fieldName, fieldValue := range o.End { + sqlBuildResultBlockList = append(sqlBuildResultBlockList, "`"+fieldName+"` < ?") + bindValue = append(bindValue, fieldValue) + } + + if len(bindValue) > 0 { + sqlBuildResult = strings.Join(sqlBuildResultBlockList, " AND ") + } + return +} diff --git a/option_test.go b/option_test.go new file mode 100644 index 0000000..db8b325 --- /dev/null +++ b/option_test.go @@ -0,0 +1,41 @@ +// Package database ... +// +// Description : database ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-08-09 18:19 +package database + +import ( + "fmt" + "testing" +) + +func Test_optionToSql(t *testing.T) { + type args struct { + o *Option + } + o := &Option{ + In: nil, + NotIn: nil, + Where: map[string]any{ + "name": []string{"zhang", "baicha"}, + "age": 18, + }, + Start: nil, + End: nil, + Like: map[string]string{ + "name": "de", + }, + NotLike: map[string]string{ + "name": "zhang", + }, + NotEqual: map[string]any{ + "a": 123, + }, + Order: nil, + OR: nil, + } + fmt.Println(optionToSql(o)) +} -- 2.36.6