Compare commits
4 Commits
ca811dbdf4
...
fa5def962c
Author | SHA1 | Date | |
---|---|---|---|
fa5def962c | |||
7126765fd5 | |||
88f9e831d8 | |||
ce2f962784 |
@ -19,17 +19,17 @@ import (
|
|||||||
// Date : 15:06 2023/10/14
|
// Date : 15:06 2023/10/14
|
||||||
type Base interface {
|
type Base interface {
|
||||||
// Create 创建数据
|
// Create 创建数据
|
||||||
Create(dbInstance *gorm.DB, data interface{}, optionList ...database.SetOption) error
|
Create(dbInstance *gorm.DB, data any, optionList ...database.SetOption) error
|
||||||
// Update 更新数据
|
// 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 更新一条数据
|
||||||
UpdateOne(dbInstance *gorm.DB, updateData interface{}, optionFuncList ...database.SetOption) (int64, error)
|
UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList ...database.SetOption) (int64, error)
|
||||||
// List 查询数据列表
|
// List 查询数据列表
|
||||||
List(dbInstance *gorm.DB, result interface{}, optionFuncList ...database.SetOption) error
|
List(dbInstance *gorm.DB, result any, optionFuncList ...database.SetOption) error
|
||||||
// Delete 删除数据
|
// 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 数据详情
|
||||||
Detail(dbInstance *gorm.DB, result interface{}, optionFuncList ...database.SetOption) error
|
Detail(dbInstance *gorm.DB, result any, optionFuncList ...database.SetOption) error
|
||||||
// IsNotFound 错误是否为数据不存在
|
// IsNotFound 错误是否为数据不存在
|
||||||
IsNotFound(err error) bool
|
IsNotFound(err error) bool
|
||||||
// Count 查询数据数量
|
// Count 查询数据数量
|
||||||
|
90
base.go
90
base.go
@ -25,7 +25,7 @@ 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 interface{}, optionList ...SetOption) error {
|
func (b *BaseDao) Create(dbInstance *gorm.DB, data any, optionList ...SetOption) error {
|
||||||
o := &Option{}
|
o := &Option{}
|
||||||
for _, itemFunc := range optionList {
|
for _, itemFunc := range optionList {
|
||||||
itemFunc(o)
|
itemFunc(o)
|
||||||
@ -42,7 +42,7 @@ func (b *BaseDao) Create(dbInstance *gorm.DB, data interface{}, optionList ...Se
|
|||||||
// 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 interface{}, optionFuncList ...SetOption) (int64, error) {
|
func (b *BaseDao) Update(dbInstance *gorm.DB, updateData any, optionFuncList ...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 +53,7 @@ func (b *BaseDao) Update(dbInstance *gorm.DB, updateData interface{}, optionFunc
|
|||||||
// 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 interface{}, optionFuncList ...SetOption) (int64, error) {
|
func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData any, optionFuncList ...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 +63,7 @@ func (b *BaseDao) UpdateOne(dbInstance *gorm.DB, updateData interface{}, optionF
|
|||||||
// 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 interface{}, optionFuncList ...SetOption) error {
|
func (b *BaseDao) List(dbInstance *gorm.DB, result any, optionFuncList ...SetOption) error {
|
||||||
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
|
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
|
||||||
return dbInstance.Find(result).Error
|
return dbInstance.Find(result).Error
|
||||||
}
|
}
|
||||||
@ -73,7 +73,7 @@ func (b *BaseDao) List(dbInstance *gorm.DB, result interface{}, optionFuncList .
|
|||||||
// 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 interface{}, optionFuncList ...SetOption) (int64, error) {
|
func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...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 +84,7 @@ func (b *BaseDao) Delete(dbInstance *gorm.DB, dataModel interface{}, optionFuncL
|
|||||||
// 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 interface{}, optionFuncList ...SetOption) error {
|
func (b *BaseDao) Detail(dbInstance *gorm.DB, result any, optionFuncList ...SetOption) error {
|
||||||
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
|
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
|
||||||
return dbInstance.First(result).Error
|
return dbInstance.First(result).Error
|
||||||
}
|
}
|
||||||
@ -94,12 +94,12 @@ func (b *BaseDao) Detail(dbInstance *gorm.DB, result interface{}, optionFuncList
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 14:14 2023/10/15
|
// 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"
|
primaryKeyField := "id"
|
||||||
if len(primaryKey) > 0 {
|
if len(primaryKey) > 0 {
|
||||||
primaryKeyField = 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,
|
primaryKeyField: primaryID,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -192,11 +192,6 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
|
|||||||
tx = tx.Model(o.Model)
|
tx = tx.Model(o.Model)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置where条件
|
|
||||||
if nil != o.Where && len(o.Where) > 0 {
|
|
||||||
tx = tx.Where(o.Where)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置 limit offset
|
// 设置 limit offset
|
||||||
if o.Limit > 0 {
|
if o.Limit > 0 {
|
||||||
tx = tx.Limit(o.Limit)
|
tx = tx.Limit(o.Limit)
|
||||||
@ -204,64 +199,19 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
|
|||||||
tx = tx.Offset(o.Offset)
|
tx = tx.Offset(o.Offset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sqlBlockList := make([]string, 0)
|
||||||
// in 语句
|
bindValueList := make([]any, 0)
|
||||||
if nil != o.In {
|
sql, bindVal := optionToSql(o)
|
||||||
tx = tx.Where(o.In)
|
tx.Or(sql, bindVal...)
|
||||||
}
|
sqlBlockList = append(sqlBlockList, sql)
|
||||||
|
bindValueList = append(bindValueList, bindVal)
|
||||||
// not in 语句
|
for _, itemOrFuncList := range o.OR {
|
||||||
if nil != o.NotIn {
|
orOption := &Option{}
|
||||||
for field, value := range o.NotIn {
|
for _, fn := range itemOrFuncList {
|
||||||
tx = tx.Where(field+" NOT IN ? ", value)
|
fn(orOption)
|
||||||
}
|
}
|
||||||
}
|
orSql, orBindVal := optionToSql(orOption)
|
||||||
|
tx.Or(orSql, orBindVal...)
|
||||||
// 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 nil != o.OR {
|
|
||||||
expression := ""
|
|
||||||
valList := make([]interface{}, 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...)
|
|
||||||
}
|
}
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
172
option.go
172
option.go
@ -8,7 +8,10 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"git.zhangdeman.cn/zhangdeman/op_type"
|
"git.zhangdeman.cn/zhangdeman/op_type"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetOption 设置选项
|
// SetOption 设置选项
|
||||||
@ -18,36 +21,26 @@ import (
|
|||||||
// Date : 11:46 2022/5/15
|
// Date : 11:46 2022/5/15
|
||||||
type SetOption func(o *Option)
|
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 interface{} `json:"value"` // 绑定值
|
|
||||||
}
|
|
||||||
|
|
||||||
// Option 扩展选项
|
// Option 扩展选项
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 8:05 下午 2021/8/8
|
// Date : 8:05 下午 2021/8/8
|
||||||
type Option struct {
|
type Option struct {
|
||||||
Model interface{} `json:"-"` // 操作model
|
Model any `json:"-"` // 操作model
|
||||||
Table string `json:"table"` // 查询的数据表
|
Table string `json:"table"` // 查询的数据表
|
||||||
Limit int `json:"limit"` // 限制数量
|
Limit int `json:"limit"` // 限制数量
|
||||||
Offset int `json:"offset"` // 偏移量
|
Offset int `json:"offset"` // 偏移量
|
||||||
In map[string]interface{} `json:"in"` // in语句
|
In map[string]any `json:"in"` // in语句
|
||||||
NotIn map[string]interface{} `json:"not_in"` // not in语句
|
NotIn map[string]any `json:"not_in"` // not in语句
|
||||||
Where map[string]interface{} `json:"where"` // where 条件
|
Where map[string]any `json:"where"` // where 条件
|
||||||
Start map[string]interface{} `json:"start"` // >= 条件
|
Start map[string]any `json:"start"` // >= 条件
|
||||||
End map[string]interface{} `json:"end"` // < 条件
|
End map[string]any `json:"end"` // < 条件
|
||||||
Like map[string]string `json:"like"` // like 语句
|
Like map[string]string `json:"like"` // like 语句
|
||||||
NotLike map[string]string `json:"not_like"` // not like 语句
|
NotLike map[string]string `json:"not_like"` // not like 语句
|
||||||
NotEqual map[string]interface{} `json:"not_equal"` // != 语句
|
NotEqual map[string]any `json:"not_equal"` // != 语句
|
||||||
Order []string `json:"order"` // 排序规则
|
Order []string `json:"order"` // 排序规则
|
||||||
OR []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
|
OR [][]SetOption `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithModel ...
|
// WithModel ...
|
||||||
@ -55,7 +48,7 @@ type Option struct {
|
|||||||
// 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 interface{}) SetOption {
|
func WithModel(model any) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
o.Model = model
|
o.Model = model
|
||||||
}
|
}
|
||||||
@ -80,7 +73,7 @@ func WithTable(tableName string) SetOption {
|
|||||||
func WithWhere[T op_type.BaseType](where map[string]T) SetOption {
|
func WithWhere[T op_type.BaseType](where map[string]T) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.Where {
|
if nil == o.Where {
|
||||||
o.Where = make(map[string]interface{})
|
o.Where = make(map[string]any)
|
||||||
}
|
}
|
||||||
for field, value := range where {
|
for field, value := range where {
|
||||||
o.Where[field] = value
|
o.Where[field] = value
|
||||||
@ -131,7 +124,7 @@ func WithIn[T op_type.Array](field string, value T) SetOption {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if nil == o.In {
|
if nil == o.In {
|
||||||
o.In = make(map[string]interface{})
|
o.In = make(map[string]any)
|
||||||
}
|
}
|
||||||
o.In[field] = value
|
o.In[field] = value
|
||||||
}
|
}
|
||||||
@ -145,7 +138,7 @@ func WithIn[T op_type.Array](field string, value T) SetOption {
|
|||||||
func WithBatchIn[T op_type.Array](batchIn map[string]T) SetOption {
|
func WithBatchIn[T op_type.Array](batchIn map[string]T) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.In {
|
if nil == o.In {
|
||||||
o.In = make(map[string]interface{})
|
o.In = make(map[string]any)
|
||||||
}
|
}
|
||||||
for field, value := range batchIn {
|
for field, value := range batchIn {
|
||||||
WithIn(field, value)
|
WithIn(field, value)
|
||||||
@ -161,7 +154,7 @@ func WithBatchIn[T op_type.Array](batchIn map[string]T) SetOption {
|
|||||||
func WithNotIn[T op_type.Array](field string, value T) SetOption {
|
func WithNotIn[T op_type.Array](field string, value T) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.NotIn {
|
if nil == o.NotIn {
|
||||||
o.NotIn = make(map[string]interface{})
|
o.NotIn = make(map[string]any)
|
||||||
}
|
}
|
||||||
if value == nil || len(value) == 0 {
|
if value == nil || len(value) == 0 {
|
||||||
return
|
return
|
||||||
@ -178,7 +171,7 @@ func WithNotIn[T op_type.Array](field string, value T) SetOption {
|
|||||||
func WithBatchNotIn[T op_type.Array](data map[string]T) SetOption {
|
func WithBatchNotIn[T op_type.Array](data map[string]T) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.NotIn {
|
if nil == o.NotIn {
|
||||||
o.NotIn = make(map[string]interface{})
|
o.NotIn = make(map[string]any)
|
||||||
}
|
}
|
||||||
for field, value := range data {
|
for field, value := range data {
|
||||||
if value == nil || len(value) == 0 {
|
if value == nil || len(value) == 0 {
|
||||||
@ -194,10 +187,10 @@ 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 interface{}) SetOption {
|
func WithStart(field string, value any) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.Start {
|
if nil == o.Start {
|
||||||
o.Start = make(map[string]interface{})
|
o.Start = make(map[string]any)
|
||||||
}
|
}
|
||||||
o.Start[field] = value
|
o.Start[field] = value
|
||||||
}
|
}
|
||||||
@ -208,10 +201,10 @@ func WithStart(field string, value interface{}) 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]interface{}) SetOption {
|
func WithBatchStart(data map[string]any) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.Start {
|
if nil == o.Start {
|
||||||
o.Start = make(map[string]interface{})
|
o.Start = make(map[string]any)
|
||||||
}
|
}
|
||||||
for field, value := range data {
|
for field, value := range data {
|
||||||
o.Start[field] = value
|
o.Start[field] = value
|
||||||
@ -224,10 +217,10 @@ func WithBatchStart(data map[string]interface{}) 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 interface{}) SetOption {
|
func WithEnd(field string, value any) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.End {
|
if nil == o.End {
|
||||||
o.End = make(map[string]interface{})
|
o.End = make(map[string]any)
|
||||||
}
|
}
|
||||||
o.End[field] = value
|
o.End[field] = value
|
||||||
}
|
}
|
||||||
@ -238,10 +231,10 @@ func WithEnd(field string, value interface{}) 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]interface{}) SetOption {
|
func WithBatchEnd(data map[string]any) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.End {
|
if nil == o.End {
|
||||||
o.End = make(map[string]interface{})
|
o.End = make(map[string]any)
|
||||||
}
|
}
|
||||||
for field, value := range data {
|
for field, value := range data {
|
||||||
o.End[field] = value
|
o.End[field] = value
|
||||||
@ -329,7 +322,7 @@ func WithBatchNotLike(data map[string]string) SetOption {
|
|||||||
func WithNotEqual[T op_type.BaseType](field string, value T) SetOption {
|
func WithNotEqual[T op_type.BaseType](field string, value T) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.NotEqual {
|
if nil == o.NotEqual {
|
||||||
o.NotEqual = make(map[string]interface{})
|
o.NotEqual = make(map[string]any)
|
||||||
}
|
}
|
||||||
o.NotEqual[field] = value
|
o.NotEqual[field] = value
|
||||||
}
|
}
|
||||||
@ -343,7 +336,7 @@ func WithNotEqual[T op_type.BaseType](field string, value T) SetOption {
|
|||||||
func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption {
|
func WithBatchNotEqual[T op_type.BaseType](data map[string]T) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.NotEqual {
|
if nil == o.NotEqual {
|
||||||
o.NotEqual = make(map[string]interface{})
|
o.NotEqual = make(map[string]any)
|
||||||
}
|
}
|
||||||
for field, value := range data {
|
for field, value := range data {
|
||||||
o.NotEqual[field] = value
|
o.NotEqual[field] = value
|
||||||
@ -356,12 +349,12 @@ 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 ...*ORCondition) SetOption {
|
func WithOR(orConditionList ...SetOption) SetOption {
|
||||||
return func(o *Option) {
|
return func(o *Option) {
|
||||||
if nil == o.OR {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -403,3 +396,96 @@ func WithOrderAsc(field string) SetOption {
|
|||||||
o.Order = append(o.Order, field+" asc")
|
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
|
||||||
|
}
|
||||||
|
41
option_test.go
Normal file
41
option_test.go
Normal file
@ -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))
|
||||||
|
}
|
@ -36,9 +36,9 @@ func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetDetailBy{PRIMARY_KEY}(dbInstance *go
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetList 获取数据列表
|
// 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 {
|
if nil == condition {
|
||||||
condition = make(map[string]interface{})
|
condition = make(map[string]any)
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
|
@ -90,9 +90,9 @@ func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetDetailBy{PRIMARY_KEY}(dbInstance *go
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetList 获取数据列表
|
// 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 {
|
if nil == condition {
|
||||||
condition = make(map[string]interface{})
|
condition = make(map[string]any)
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
|
@ -47,9 +47,9 @@ func (ad *AppDao) GetDetailByID(dbInstance *gorm.DB, ID int64) (*App, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetList 获取数据列表
|
// 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 {
|
if nil == condition {
|
||||||
condition = make(map[string]interface{})
|
condition = make(map[string]any)
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
|
@ -85,7 +85,7 @@ func (sd *SystemDao) GetTableList(dbInstance *gorm.DB) ([]string, error) {
|
|||||||
func (sd *SystemDao) GetCreateTableSQL(dbInstance *gorm.DB, table string) (string, error) {
|
func (sd *SystemDao) GetCreateTableSQL(dbInstance *gorm.DB, table string) (string, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
result map[string]interface{}
|
result map[string]any
|
||||||
)
|
)
|
||||||
|
|
||||||
if err = dbInstance.Raw("SHOW CREATE TABLE `" + table + "`").Scan(&result).Error; nil != err {
|
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<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 16:35 2023/9/30
|
// 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
|
return dbInstance.Exec(anySql, value...).Error
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user