From 9806f42fff234fb833cf1464d536698583d619e0 Mon Sep 17 00:00:00 2001 From: xuthus5 Date: Sun, 17 Sep 2023 11:03:08 +0800 Subject: [PATCH] feat: fund operator support count fix: set where condition --- delete_scope.go | 3 ++- find_scope.go | 59 +++++++++++++++++++++++++++++++++++++++---------- update_scope.go | 3 ++- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/delete_scope.go b/delete_scope.go index caf315a..18a07fa 100644 --- a/delete_scope.go +++ b/delete_scope.go @@ -15,7 +15,8 @@ type _delete struct { // SetWhere 设置删除条件 func (d *_delete) SetWhere(query any, args ...any) *_delete { - d.where = append(d.where, query, args) + d.where = append(d.where, query) + d.where = append(d.where, args...) return d } diff --git a/find_scope.go b/find_scope.go index 552f68e..88fb4f2 100644 --- a/find_scope.go +++ b/find_scope.go @@ -7,22 +7,24 @@ import ( ) type _find struct { - scope *scope - sdbc *Driver - ctx context.Context - where []any - selectOpts []interface{} - omitOpts []string - notOpts []interface{} - orOpts []interface{} - orderOpts string - limit int - offset int + 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, args) + f.where = append(f.where, query) + f.where = append(f.where, args...) return f } @@ -68,6 +70,12 @@ func (f *_find) SetOrder(order string) *_find { 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 { // 不是结构体指针 返回错误 @@ -117,5 +125,32 @@ func (f *_find) Find(binds any) error { 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 +} diff --git a/update_scope.go b/update_scope.go index b6a4695..1429a0c 100644 --- a/update_scope.go +++ b/update_scope.go @@ -16,7 +16,8 @@ type _update struct { // SetWhere 设置更新条件 func (u *_update) SetWhere(query any, args ...any) *_update { - u.where = append(u.where, query, args) + u.where = append(u.where, query) + u.where = append(u.where, args...) return u }