2021-01-19 12:26:26 +00:00
|
|
|
package mongo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-05-03 07:12:58 +00:00
|
|
|
"sort"
|
2021-01-19 12:35:54 +00:00
|
|
|
"strings"
|
2021-01-19 12:26:26 +00:00
|
|
|
|
|
|
|
"github.com/sunboyy/repogen/internal/spec"
|
|
|
|
)
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
type updateField struct {
|
|
|
|
BsonTag string
|
|
|
|
ParamIndex int
|
|
|
|
}
|
|
|
|
|
2021-02-24 12:02:57 +00:00
|
|
|
type update interface {
|
|
|
|
Code() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateModel struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u updateModel) Code() string {
|
|
|
|
return ` "$set": arg1,`
|
|
|
|
}
|
|
|
|
|
2021-03-31 11:44:31 +00:00
|
|
|
type updateFields map[string][]updateField
|
2021-02-24 12:02:57 +00:00
|
|
|
|
|
|
|
func (u updateFields) Code() string {
|
2021-05-03 07:12:58 +00:00
|
|
|
var keys []string
|
|
|
|
for k := range u {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
2021-03-31 11:44:31 +00:00
|
|
|
var lines []string
|
2021-05-03 07:12:58 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
lines = append(lines, fmt.Sprintf(` "%s": bson.M{`, key))
|
2021-03-31 11:44:31 +00:00
|
|
|
|
2021-05-03 07:12:58 +00:00
|
|
|
for _, field := range u[key] {
|
2021-03-31 11:44:31 +00:00
|
|
|
lines = append(lines, fmt.Sprintf(` "%s": arg%d,`, field.BsonTag, field.ParamIndex))
|
|
|
|
}
|
|
|
|
|
|
|
|
lines = append(lines, ` },`)
|
2021-02-24 12:02:57 +00:00
|
|
|
}
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
2021-01-19 12:35:54 +00:00
|
|
|
type querySpec struct {
|
|
|
|
Operator spec.Operator
|
|
|
|
Predicates []predicate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q querySpec) Code() string {
|
|
|
|
var predicateCodes []string
|
2021-01-24 08:31:21 +00:00
|
|
|
for _, predicate := range q.Predicates {
|
2021-01-27 12:15:25 +00:00
|
|
|
predicateCodes = append(predicateCodes, predicate.Code())
|
2021-01-19 12:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
switch q.Operator {
|
|
|
|
case spec.OperatorOr:
|
|
|
|
lines = append(lines, ` "$or": []bson.M{`)
|
|
|
|
for _, predicateCode := range predicateCodes {
|
|
|
|
lines = append(lines, fmt.Sprintf(` {%s},`, predicateCode))
|
|
|
|
}
|
|
|
|
lines = append(lines, ` },`)
|
2021-02-12 15:19:31 +00:00
|
|
|
case spec.OperatorAnd:
|
|
|
|
lines = append(lines, ` "$and": []bson.M{`)
|
|
|
|
for _, predicateCode := range predicateCodes {
|
|
|
|
lines = append(lines, fmt.Sprintf(` {%s},`, predicateCode))
|
|
|
|
}
|
|
|
|
lines = append(lines, ` },`)
|
2021-01-19 12:35:54 +00:00
|
|
|
default:
|
|
|
|
for _, predicateCode := range predicateCodes {
|
|
|
|
lines = append(lines, fmt.Sprintf(` %s,`, predicateCode))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
2021-01-19 12:26:26 +00:00
|
|
|
type predicate struct {
|
2021-01-19 12:35:54 +00:00
|
|
|
Field string
|
|
|
|
Comparator spec.Comparator
|
2021-01-27 12:15:25 +00:00
|
|
|
ParamIndex int
|
2021-01-19 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
func (p predicate) Code() string {
|
2021-01-19 12:35:54 +00:00
|
|
|
switch p.Comparator {
|
|
|
|
case spec.ComparatorEqual:
|
2021-01-27 12:15:25 +00:00
|
|
|
return fmt.Sprintf(`"%s": arg%d`, p.Field, p.ParamIndex)
|
2021-01-19 12:35:54 +00:00
|
|
|
case spec.ComparatorNot:
|
2021-01-27 12:15:25 +00:00
|
|
|
return fmt.Sprintf(`"%s": bson.M{"$ne": arg%d}`, p.Field, p.ParamIndex)
|
2021-01-19 12:35:54 +00:00
|
|
|
case spec.ComparatorLessThan:
|
2021-01-27 12:15:25 +00:00
|
|
|
return fmt.Sprintf(`"%s": bson.M{"$lt": arg%d}`, p.Field, p.ParamIndex)
|
2021-01-19 12:35:54 +00:00
|
|
|
case spec.ComparatorLessThanEqual:
|
2021-01-27 12:15:25 +00:00
|
|
|
return fmt.Sprintf(`"%s": bson.M{"$lte": arg%d}`, p.Field, p.ParamIndex)
|
2021-01-19 12:35:54 +00:00
|
|
|
case spec.ComparatorGreaterThan:
|
2021-01-27 12:15:25 +00:00
|
|
|
return fmt.Sprintf(`"%s": bson.M{"$gt": arg%d}`, p.Field, p.ParamIndex)
|
2021-01-19 12:35:54 +00:00
|
|
|
case spec.ComparatorGreaterThanEqual:
|
2021-01-27 12:15:25 +00:00
|
|
|
return fmt.Sprintf(`"%s": bson.M{"$gte": arg%d}`, p.Field, p.ParamIndex)
|
2021-01-24 08:31:21 +00:00
|
|
|
case spec.ComparatorBetween:
|
2021-01-27 12:15:25 +00:00
|
|
|
return fmt.Sprintf(`"%s": bson.M{"$gte": arg%d, "$lte": arg%d}`, p.Field, p.ParamIndex, p.ParamIndex+1)
|
2021-01-22 02:56:30 +00:00
|
|
|
case spec.ComparatorIn:
|
2021-01-27 12:15:25 +00:00
|
|
|
return fmt.Sprintf(`"%s": bson.M{"$in": arg%d}`, p.Field, p.ParamIndex)
|
2021-02-23 12:10:25 +00:00
|
|
|
case spec.ComparatorNotIn:
|
|
|
|
return fmt.Sprintf(`"%s": bson.M{"$nin": arg%d}`, p.Field, p.ParamIndex)
|
|
|
|
case spec.ComparatorTrue:
|
|
|
|
return fmt.Sprintf(`"%s": true`, p.Field)
|
|
|
|
case spec.ComparatorFalse:
|
|
|
|
return fmt.Sprintf(`"%s": false`, p.Field)
|
2021-01-19 12:26:26 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|