升级基础sql构建, 支持OR语句 #6

Merged
zhangdeman merged 3 commits from feture/support_or into master 2024-08-09 22:11:53 +08:00
7 changed files with 55 additions and 55 deletions
Showing only changes of commit ce2f962784 - Show all commits

View File

@ -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 查询数据数量

18
base.go
View File

@ -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,
})) }))
} }
@ -253,7 +253,7 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
// or 语句 // or 语句
if nil != o.OR { if nil != o.OR {
expression := "" expression := ""
valList := make([]interface{}, 0) valList := make([]any, 0)
for _, o := range o.OR { for _, o := range o.OR {
if len(expression) > 0 { if len(expression) > 0 {
expression = expression + " OR " expression = expression + " OR "

View File

@ -25,7 +25,7 @@ type SetOption func(o *Option)
// Date : 20:07 2022/7/23 // Date : 20:07 2022/7/23
type ORCondition struct { type ORCondition struct {
Express string `json:"express"` // 表达式 Express string `json:"express"` // 表达式
Value interface{} `json:"value"` // 绑定值 Value any `json:"value"` // 绑定值
} }
// Option 扩展选项 // Option 扩展选项
@ -34,18 +34,18 @@ type ORCondition struct {
// //
// 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 []*ORCondition `json:"or"` // or 语句, or 和其他属性 and 关系, 内部 OR 关系
} }
@ -55,7 +55,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 +80,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 +131,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 +145,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 +161,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 +178,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 +194,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 +208,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 +224,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 +238,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 +329,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 +343,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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
} }