sdbc/find_scope.go

188 lines
4.2 KiB
Go

package sdbc
import (
"context"
"gitter.top/common/goref"
"reflect"
)
type _find struct {
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
}
// SetWhere 设置更新条件
func (f *_find) SetWhere(query any, args ...any) *_find {
f.where = append(f.where, query)
f.where = append(f.where, args...)
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
}
// SetDistinct 字段去重
func (f *_find) SetDistinct(distinct ...interface{}) *_find {
f.distinctOpts = distinct
return f
}
// 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
}
// 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
}
// 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:]...)
}
if len(f.distinctOpts) != 0 {
db = db.Distinct(f.distinctOpts...)
}
return db.Model(f.scope.model.ptr()).Find(binds).Error
}
// 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
}