1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
query_builder.go 4.50 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.3

搜索帮助

0d507c66 1850385 C8b1a773 1850385