代码拉取完成,页面将自动刷新
package mongo
import (
"log"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
type WhereOption struct {
Attr string
Predicate Predicate
Value interface{}
}
type OrderOption struct {
Attr string
Order OrderType
}
type ModelQueryBuilder[T interface{}] struct {
order []OrderOption
where []WhereOption
limit int64
offset int64
}
func (qb *ModelQueryBuilder[T]) AddWhere(attr string, value interface{}, predicate Predicate) *ModelQueryBuilder[T] {
qb.where = append(qb.where, WhereOption{attr, predicate, value})
return qb
}
// or/and logic
func (qb *ModelQueryBuilder[T]) AddWhereLogic(value interface{}, logicPredicate Predicate) *ModelQueryBuilder[T] {
qb.where = append(qb.where, WhereOption{"", logicPredicate, value})
return qb
}
func (qb *ModelQueryBuilder[T]) AddWhereAnd(value interface{}) *ModelQueryBuilder[T] {
qb.where = append(qb.where, WhereOption{"", AndPredicate, value})
return qb
}
func (qb *ModelQueryBuilder[T]) AddWhereOr(value interface{}) *ModelQueryBuilder[T] {
qb.where = append(qb.where, WhereOption{"", OrPredicate, value})
return qb
}
func (qb *ModelQueryBuilder[T]) AddWhereByOpt(w *WhereOption) *ModelQueryBuilder[T] {
qb.where = append(qb.where, *w)
return qb
}
func (qb *ModelQueryBuilder[T]) ClearWheres() *ModelQueryBuilder[T] {
qb.where = []WhereOption{}
return qb
}
func (qb *ModelQueryBuilder[T]) BuildQuery() *bson.M {
filter := bson.M{}
for _, where := range qb.where {
if where.Predicate == EqualPredicate {
filter[where.Attr] = where.Value
} else if where.Predicate == NotEqualPredicate ||
where.Predicate == GreaterThanPredicate ||
where.Predicate == GreaterThanOrEqualPredicate ||
where.Predicate == SmallerThanPredicate ||
where.Predicate == SmallerThanOrEqualPredicate {
m := bson.M{}
m[string(where.Predicate)] = where.Value
filter[where.Attr] = m
} else if where.Predicate == AndPredicate ||
where.Predicate == OrPredicate {
filter[string(where.Predicate)] = where.Value
} else {
log.Println("unsupported predicate:" + string(where.Predicate))
}
}
return &filter
}
func (qb *ModelQueryBuilder[T]) Updates(col MongoCollInterface[T], m map[string]interface{}) (err error) {
filter := qb.BuildQuery()
if err = col.UpdateOneByFilter(filter, m); err != nil {
return errors.Wrap(err, "updates err")
}
return nil
}
func (qb *ModelQueryBuilder[T]) Delete(col MongoCollInterface[T]) (err error) {
filter := qb.BuildQuery()
if err = col.DeleteAllByFilter(&filter); err != nil {
return errors.Wrap(err, "delete err")
}
return nil
}
func (qb *ModelQueryBuilder[T]) Count(col MongoCollInterface[T]) (int64, error) {
filter := qb.BuildQuery()
return col.Count(filter)
}
func (qb *ModelQueryBuilder[T]) First(col MongoCollInterface[T]) (*T, error) {
filter := qb.BuildQuery()
return col.FindOneByFilter(filter)
}
func (qb *ModelQueryBuilder[T]) QueryOne(col MongoCollInterface[T]) (*T, error) {
return qb.First(col)
}
func (qb *ModelQueryBuilder[T]) QueryAll(col MongoCollInterface[T]) ([]T, error) {
filter := qb.BuildQuery()
if qb.offset != 0 && qb.limit != 0 {
opts := &options.FindOptions{
Skip: &qb.offset,
Limit: &qb.limit,
}
return col.FindAllByFilter(filter, opts)
}
return col.FindAllByFilter(filter)
}
func (qb *ModelQueryBuilder[T]) Limit(limit int64) *ModelQueryBuilder[T] {
qb.limit = limit
return qb
}
func (qb *ModelQueryBuilder[T]) Offset(offset int64) *ModelQueryBuilder[T] {
qb.offset = offset
return qb
}
func (qb *ModelQueryBuilder[T]) WhereId(p Predicate, value string) *ModelQueryBuilder[T] {
qb.where = append(qb.where, WhereOption{"id", p, value})
return qb
}
func (qb *ModelQueryBuilder[T]) WhereIdIn(value []string) *ModelQueryBuilder[T] {
qb.where = append(qb.where, WhereOption{"id", InPredicate, value})
return qb
}
func (qb *ModelQueryBuilder[T]) OrderById(asc bool) *ModelQueryBuilder[T] {
qb.AddOrder("id", asc)
return qb
}
func (qb *ModelQueryBuilder[T]) AddOrder(attr string, asc bool) *ModelQueryBuilder[T] {
order := OrderDesc
if asc {
order = OrderAsc
}
qb.order = append(qb.order, OrderOption{attr, order})
return qb
}
func (qb *ModelQueryBuilder[T]) AddOrder2(attr string, order OrderType) *ModelQueryBuilder[T] {
qb.order = append(qb.order, OrderOption{attr, order})
return qb
}
func (qb *ModelQueryBuilder[T]) AddOrderByOpt(w *OrderOption) *ModelQueryBuilder[T] {
qb.order = append(qb.order, *w)
return qb
}
func (qb *ModelQueryBuilder[T]) ClearOrders() *ModelQueryBuilder[T] {
qb.order = []OrderOption{}
return qb
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。