2023-09-16 10:09:34 +00:00
|
|
|
package sdbc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"gitter.top/common/goref"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type _find struct {
|
2023-09-17 03:03:08 +00:00
|
|
|
scope *scope
|
|
|
|
sdbc *Driver
|
|
|
|
ctx context.Context
|
|
|
|
where []any
|
|
|
|
selectOpts []interface{}
|
|
|
|
omitOpts []string
|
|
|
|
notOpts []interface{}
|
|
|
|
orOpts []interface{}
|
|
|
|
distinctOpts []interface{}
|
|
|
|
orderOpts string
|
|
|
|
limit int
|
|
|
|
offset int
|
2023-09-16 10:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetWhere 设置更新条件
|
|
|
|
func (f *_find) SetWhere(query any, args ...any) *_find {
|
2023-09-17 03:03:08 +00:00
|
|
|
f.where = append(f.where, query)
|
|
|
|
f.where = append(f.where, args...)
|
2023-09-16 10:09:34 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOr 设置或条件
|
|
|
|
func (f *_find) SetOr(query any, args ...any) *_find {
|
|
|
|
f.orOpts = append(f.orOpts, query, args)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNot 设置非条件
|
|
|
|
func (f *_find) SetNot(query any, args ...any) *_find {
|
|
|
|
f.notOpts = append(f.notOpts, query, args)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSelect 只更新字段 对Updates有效
|
|
|
|
func (f *_find) SetSelect(selects ...any) *_find {
|
|
|
|
f.selectOpts = selects
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOmit 忽略字段 对Updates有效
|
|
|
|
func (f *_find) SetOmit(omits ...string) *_find {
|
|
|
|
f.omitOpts = omits
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLimit 限制条数
|
|
|
|
func (f *_find) SetLimit(limit int) *_find {
|
|
|
|
f.limit = limit
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOffset 跳跃条数
|
|
|
|
func (f *_find) SetOffset(offset int) *_find {
|
|
|
|
f.offset = offset
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOrder 跳跃条数
|
|
|
|
func (f *_find) SetOrder(order string) *_find {
|
|
|
|
f.orderOpts = order
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2023-09-17 03:03:08 +00:00
|
|
|
// SetDistinct 字段去重
|
|
|
|
func (f *_find) SetDistinct(distinct ...interface{}) *_find {
|
|
|
|
f.distinctOpts = distinct
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2023-09-16 10:09:34 +00:00
|
|
|
// FindById 主键检索
|
|
|
|
func (f *_find) FindById(id any, bind any) error {
|
|
|
|
// 不是结构体指针 返回错误
|
|
|
|
if !goref.IsPointer(bind) && !goref.IsBaseStruct(bind) {
|
|
|
|
return ErrorUnavailableType
|
|
|
|
}
|
|
|
|
db := f.sdbc.client
|
|
|
|
return db.Model(f.scope.model.ptr()).First(bind, id).Error
|
|
|
|
}
|
|
|
|
|
2023-09-29 06:32:42 +00:00
|
|
|
// FindOne 查找一条
|
|
|
|
func (f *_find) FindOne(bind any) error {
|
|
|
|
// 不是结构体指针 返回错误
|
|
|
|
if !goref.IsPointer(bind) && !goref.IsBaseStruct(bind) {
|
|
|
|
return ErrorUnavailableType
|
|
|
|
}
|
|
|
|
db := f.sdbc.client
|
|
|
|
if len(f.where) != 0 {
|
|
|
|
db = db.Where(f.where[0], f.where[1:]...)
|
|
|
|
}
|
|
|
|
if f.limit != 0 {
|
|
|
|
db = db.Limit(f.limit)
|
|
|
|
}
|
|
|
|
if f.offset != 0 {
|
|
|
|
db = db.Offset(f.offset)
|
|
|
|
}
|
|
|
|
if f.orderOpts != "" {
|
|
|
|
db = db.Order(f.orderOpts)
|
|
|
|
}
|
|
|
|
if len(f.orOpts) != 0 {
|
|
|
|
db = db.Or(f.orOpts[0], f.orOpts[1:]...)
|
|
|
|
}
|
|
|
|
if len(f.notOpts) != 0 {
|
|
|
|
db = db.Not(f.notOpts[0], f.notOpts[1:]...)
|
|
|
|
}
|
|
|
|
if len(f.distinctOpts) != 0 {
|
|
|
|
db = db.Distinct(f.distinctOpts...)
|
|
|
|
}
|
|
|
|
return db.Model(f.scope.model.ptr()).First(bind).Error
|
|
|
|
}
|
|
|
|
|
2023-09-16 10:09:34 +00:00
|
|
|
// FindByIds 主键检索
|
|
|
|
func (f *_find) FindByIds(ids any, binds any) error {
|
|
|
|
// 不是一个id列表
|
|
|
|
if !goref.IsBaseList(ids) {
|
|
|
|
return ErrorUnavailableType
|
|
|
|
}
|
|
|
|
// 不是结构体指针 返回错误
|
|
|
|
if !goref.IsBaseList(binds) && goref.GetBaseType(binds).Kind() != reflect.Struct {
|
|
|
|
return ErrorUnavailableType
|
|
|
|
}
|
|
|
|
db := f.sdbc.client
|
|
|
|
return db.Model(f.scope.model.ptr()).Where(ids).Find(binds).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find 使用where条件查询
|
|
|
|
func (f *_find) Find(binds any) error {
|
|
|
|
// 不是结构体指针 返回错误
|
|
|
|
if !goref.IsPointer(binds) || !goref.IsBaseList(binds) || goref.GetBaseType(binds).Kind() != reflect.Struct {
|
|
|
|
return ErrorUnavailableType
|
|
|
|
}
|
|
|
|
db := f.sdbc.client
|
|
|
|
if len(f.where) != 0 {
|
|
|
|
db = db.Where(f.where[0], f.where[1:]...)
|
|
|
|
}
|
|
|
|
if f.limit != 0 {
|
|
|
|
db = db.Limit(f.limit)
|
|
|
|
}
|
|
|
|
if f.offset != 0 {
|
|
|
|
db = db.Offset(f.offset)
|
|
|
|
}
|
|
|
|
if f.orderOpts != "" {
|
|
|
|
db = db.Order(f.orderOpts)
|
|
|
|
}
|
|
|
|
if len(f.orOpts) != 0 {
|
|
|
|
db = db.Or(f.orOpts[0], f.orOpts[1:]...)
|
|
|
|
}
|
|
|
|
if len(f.notOpts) != 0 {
|
|
|
|
db = db.Not(f.notOpts[0], f.notOpts[1:]...)
|
|
|
|
}
|
2023-09-17 03:03:08 +00:00
|
|
|
if len(f.distinctOpts) != 0 {
|
|
|
|
db = db.Distinct(f.distinctOpts...)
|
|
|
|
}
|
2023-09-16 10:09:34 +00:00
|
|
|
return db.Model(f.scope.model.ptr()).Find(binds).Error
|
|
|
|
}
|
2023-09-17 03:03:08 +00:00
|
|
|
|
|
|
|
// Count 获取匹配的记录数
|
|
|
|
func (f *_find) Count(count *int64) error {
|
|
|
|
db := f.sdbc.client
|
|
|
|
if len(f.where) != 0 {
|
|
|
|
db = db.Where(f.where[0], f.where[1:]...)
|
|
|
|
}
|
|
|
|
if f.limit != 0 {
|
|
|
|
db = db.Limit(f.limit)
|
|
|
|
}
|
|
|
|
if f.offset != 0 {
|
|
|
|
db = db.Offset(f.offset)
|
|
|
|
}
|
|
|
|
if f.orderOpts != "" {
|
|
|
|
db = db.Order(f.orderOpts)
|
|
|
|
}
|
|
|
|
if len(f.orOpts) != 0 {
|
|
|
|
db = db.Or(f.orOpts[0], f.orOpts[1:]...)
|
|
|
|
}
|
|
|
|
if len(f.notOpts) != 0 {
|
|
|
|
db = db.Not(f.notOpts[0], f.notOpts[1:]...)
|
|
|
|
}
|
|
|
|
return db.Model(f.scope.model.ptr()).Count(count).Error
|
|
|
|
}
|