mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-05-22 15:53:36 +00:00
Basic stuff, loadOne
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"github.com/mainnika/mongox-go-driver/mongox/errors"
|
||||
"github.com/mongodb/mongo-go-driver/bson"
|
||||
)
|
||||
|
||||
// ComposeQuery is a function to compose filters into a single query
|
||||
func Compose(filters ...interface{}) *Query {
|
||||
|
||||
q := &Query{}
|
||||
|
||||
for _, f := range filters {
|
||||
|
||||
ok := false
|
||||
ok = ok || applyBson(q, f)
|
||||
ok = ok || applyLimits(q, f)
|
||||
|
||||
if !ok {
|
||||
panic(errors.InternalErrorf("unknown filter %v", f))
|
||||
}
|
||||
}
|
||||
|
||||
return q
|
||||
}
|
||||
|
||||
// applyBson is a fallback for a custom bson.M
|
||||
func applyBson(q *Query, f interface{}) bool {
|
||||
|
||||
switch f := f.(type) {
|
||||
case bson.M:
|
||||
q.And(f)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// applyLimits extends query with contol functions
|
||||
func applyLimits(q *Query, f interface{}) bool {
|
||||
|
||||
switch f := f.(type) {
|
||||
case Limiter:
|
||||
q.limiter = f
|
||||
case Sorter:
|
||||
q.sorter = f
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"github.com/mongodb/mongo-go-driver/bson"
|
||||
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Query is an enchanched bson.M map
|
||||
type Query struct {
|
||||
m bson.M
|
||||
limiter Limiter
|
||||
sorter Sorter
|
||||
}
|
||||
|
||||
// And function pushes the elem query to the $and array of the query
|
||||
func (q *Query) And(elem bson.M) *Query {
|
||||
|
||||
if q.m == nil {
|
||||
q.m = bson.M{}
|
||||
}
|
||||
|
||||
queries, exists := q.m["$and"].(bson.A)
|
||||
|
||||
if !exists {
|
||||
q.m["$and"] = bson.A{elem}
|
||||
return q
|
||||
}
|
||||
|
||||
q.m["$and"] = append(queries, elem)
|
||||
|
||||
return q
|
||||
}
|
||||
|
||||
// Limiter is a limit function for a query
|
||||
func (q *Query) Limiter() Limiter {
|
||||
|
||||
return q.limiter
|
||||
}
|
||||
|
||||
// Sorter is a sort rule for a query
|
||||
func (q *Query) Sorter() Sorter {
|
||||
|
||||
return q.sorter
|
||||
}
|
||||
|
||||
// Empty checks the query for any content
|
||||
func (q *Query) Empty() bool {
|
||||
|
||||
qv := reflect.ValueOf(q)
|
||||
keys := qv.MapKeys()
|
||||
|
||||
return len(keys) == 0
|
||||
}
|
||||
|
||||
// M returns underlying query map
|
||||
func (q *Query) M() bson.M {
|
||||
|
||||
return q.m
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"github.com/mongodb/mongo-go-driver/bson"
|
||||
)
|
||||
|
||||
// Sorter is a filter to sort the data before query
|
||||
type Sorter interface {
|
||||
Sort() bson.M
|
||||
}
|
||||
|
||||
// Sort is a simple implementations of the Sorter filter
|
||||
type Sort bson.M
|
||||
|
||||
var _ Sorter = &Sort{}
|
||||
|
||||
// Sort returns a slice of fields which have to be sorted
|
||||
func (f Sort) Sort() bson.M {
|
||||
return bson.M(f)
|
||||
}
|
||||
Reference in New Issue
Block a user