2024-08-09 15:00:52 +00:00
|
|
|
package builder
|
2024-08-08 21:44:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2024-08-29 19:10:29 +00:00
|
|
|
"l12.xyz/x/dal/pkg/filters"
|
2024-08-08 21:44:51 +00:00
|
|
|
)
|
|
|
|
|
2024-08-12 18:21:34 +00:00
|
|
|
type Values = []interface{}
|
|
|
|
|
|
|
|
func covertFind(ctx Dialect, find Find) (string, Values) {
|
2024-08-08 22:27:26 +00:00
|
|
|
return covert_find(ctx, find, "")
|
2024-08-08 21:44:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 18:21:34 +00:00
|
|
|
func covert_find(ctx Dialect, find Find, join string) (string, Values) {
|
2024-08-08 21:44:51 +00:00
|
|
|
if join == "" {
|
|
|
|
join = " AND "
|
|
|
|
}
|
2024-08-09 19:14:28 +00:00
|
|
|
keys := aggregateSortedKeys([]Map{find})
|
2024-08-08 21:44:51 +00:00
|
|
|
expressions := []string{}
|
2024-08-12 18:21:34 +00:00
|
|
|
values := Values{}
|
2024-08-09 15:06:15 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
value := find[key]
|
2024-08-08 21:44:51 +00:00
|
|
|
if strings.Contains(key, "$and") {
|
2024-08-12 18:21:34 +00:00
|
|
|
exp, vals := covert_find(ctx, value.(Find), "")
|
|
|
|
values = append(values, vals...)
|
|
|
|
expressions = append(expressions, fmt.Sprintf("(%s)", exp))
|
2024-08-08 21:44:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(key, "$or") {
|
2024-08-12 18:21:34 +00:00
|
|
|
exp, vals := covert_find(ctx, value.(Find), " OR ")
|
|
|
|
values = append(values, vals...)
|
|
|
|
expressions = append(expressions, fmt.Sprintf("(%s)", exp))
|
2024-08-08 21:44:51 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-08-11 19:49:19 +00:00
|
|
|
context := ctx.New(DialectOpts{
|
2024-08-08 21:44:51 +00:00
|
|
|
"FieldName": key,
|
|
|
|
})
|
2024-08-12 18:21:34 +00:00
|
|
|
expr, vals := filters.Convert(context, value)
|
|
|
|
values = append(values, vals...)
|
|
|
|
expressions = append(expressions, expr)
|
2024-08-08 21:44:51 +00:00
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
return strings.Join(expressions, join), values
|
2024-08-08 21:44:51 +00:00
|
|
|
}
|