2024-08-09 15:00:52 +00:00
|
|
|
package builder
|
2024-08-08 21:44:51 +00:00
|
|
|
|
2024-08-08 22:27:26 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
2024-08-08 21:44:51 +00:00
|
|
|
|
|
|
|
type Join struct {
|
|
|
|
For string `json:"$for"`
|
|
|
|
Do Find `json:"$do"`
|
|
|
|
As string `json:"$as"`
|
|
|
|
}
|
|
|
|
|
2024-08-12 18:21:34 +00:00
|
|
|
func (j Join) Convert(ctx Dialect) (string, Values) {
|
2024-08-08 21:44:51 +00:00
|
|
|
if j.For == "" {
|
2024-08-12 18:21:34 +00:00
|
|
|
return "", nil
|
2024-08-08 21:44:51 +00:00
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
filter, values := covertFind(ctx, j.Do)
|
2024-08-08 22:27:26 +00:00
|
|
|
var as string = ""
|
|
|
|
if j.As != "" {
|
|
|
|
as = fmt.Sprintf("%s ", j.As)
|
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
return as + fmt.Sprintf("JOIN %s ON %s", j.For, filter), values
|
2024-08-08 22:27:26 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 18:21:34 +00:00
|
|
|
func convertJoin(ctx Dialect, joins ...interface{}) ([]string, Values) {
|
2024-08-08 22:27:26 +00:00
|
|
|
var result []string
|
2024-08-12 18:21:34 +00:00
|
|
|
var values Values
|
2024-08-08 22:27:26 +00:00
|
|
|
for _, join := range joins {
|
|
|
|
jstr, ok := join.(string)
|
|
|
|
if ok {
|
|
|
|
jjson := Join{}
|
|
|
|
err := json.Unmarshal([]byte(jstr), &jjson)
|
|
|
|
if err == nil {
|
2024-08-12 18:21:34 +00:00
|
|
|
r, vals := jjson.Convert(ctx)
|
|
|
|
result = append(result, r)
|
|
|
|
values = append(values, vals...)
|
2024-08-08 22:27:26 +00:00
|
|
|
}
|
|
|
|
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 {
|
2024-08-12 18:21:34 +00:00
|
|
|
r, vals := jjson.Convert(ctx)
|
|
|
|
result = append(result, r)
|
|
|
|
values = append(values, vals...)
|
2024-08-08 22:27:26 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
j, ok := join.(Join)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
r, vals := j.Convert(ctx)
|
|
|
|
result = append(result, r)
|
|
|
|
values = append(values, vals...)
|
2024-08-08 22:27:26 +00:00
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
return result, values
|
2024-08-08 21:44:51 +00:00
|
|
|
}
|