feat: find support find one

This commit is contained in:
xuthus5 2023-09-29 14:32:42 +08:00
parent 9806f42fff
commit b4baa9aa7f
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
1 changed files with 31 additions and 0 deletions

View File

@ -86,6 +86,37 @@ func (f *_find) FindById(id any, bind any) error {
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列表