dal/pkg/builder/convert_join.go
Anton Nesterov 43dd4e9234
[ref] name things what they are
Signed-off-by: Anton Nesterov <anton@demiurg.io>
2024-08-11 21:49:49 +02:00

59 lines
1.1 KiB
Go

package builder
import (
"encoding/json"
"fmt"
)
type Join struct {
For string `json:"$for"`
Do Find `json:"$do"`
As string `json:"$as"`
}
func (j Join) Convert(ctx Dialect) string {
if j.For == "" {
return ""
}
filter := covertFind(ctx, j.Do)
var as string = ""
if j.As != "" {
as = fmt.Sprintf("%s ", j.As)
}
return as + fmt.Sprintf("JOIN %s ON %s", j.For, filter)
}
func convertJoin(ctx Dialect, joins ...interface{}) []string {
var result []string
for _, join := range joins {
jstr, ok := join.(string)
if ok {
jjson := Join{}
err := json.Unmarshal([]byte(jstr), &jjson)
if err == nil {
result = append(result, jjson.Convert(ctx))
}
continue
}
jmap, ok := join.(map[string]interface{})
if ok {
jjson := Join{}
jstr, err := json.Marshal(jmap)
if err != nil {
continue
}
err = json.Unmarshal(jstr, &jjson)
if err == nil {
result = append(result, jjson.Convert(ctx))
}
continue
}
j, ok := join.(Join)
if !ok {
continue
}
result = append(result, j.Convert(ctx))
}
return result
}