wp-go/model/relation.go

160 lines
3.6 KiB
Go
Raw Normal View History

2023-05-17 14:22:31 +00:00
package model
import (
"context"
2023-05-19 15:14:58 +00:00
"database/sql"
2023-05-17 14:22:31 +00:00
"fmt"
2023-05-18 14:27:28 +00:00
"github.com/fthvgb1/wp-go/helper"
2023-05-20 17:38:19 +00:00
"github.com/fthvgb1/wp-go/helper/slice"
"golang.org/x/exp/constraints"
2023-05-17 14:22:31 +00:00
"strings"
)
func setTable[T Model](q *QueryCondition) {
if q.From == "" {
q.From = Table[T]()
}
}
2023-05-19 15:14:58 +00:00
type Relationship struct {
RelationType string
Table string
ForeignKey string
Local string
On string
}
2023-05-20 17:38:19 +00:00
func Relation(isMultiple bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) ([]func(), []func() error) {
2023-05-18 14:27:28 +00:00
var fn []func()
var fns []func() error
2023-05-19 15:14:58 +00:00
for _, f := range q.RelationFn {
getVal, isJoin, qq, ff := f()
2023-05-20 17:38:19 +00:00
idFn, assignment, rr, rrs, ship := ff()
2023-05-19 15:14:58 +00:00
if isJoin {
fn = append(fn, func() {
tables := strings.Split(ship.Table, " ")
from := strings.Split(q.From, " ")
on := ""
if ship.On != "" {
on = fmt.Sprintf("and %s", on)
}
qq := helper.GetContextVal(ctx, "ancestorsQueryCondition", q)
qq.Join = append(qq.Join, []string{
"left join", ship.Table, fmt.Sprintf("%s.%s=%s.%s %s", tables[len(tables)-1], ship.ForeignKey, from[len(from)-1], ship.Local, on)})
})
}
if !getVal {
2023-05-17 14:22:31 +00:00
continue
}
2023-05-19 15:14:58 +00:00
fns = append(fns, func() error {
2023-05-20 17:38:19 +00:00
ids := idFn(r)
if len(ids) < 1 {
return nil
}
2023-05-19 15:14:58 +00:00
var err error
{
if qq == nil {
qq = &QueryCondition{
Fields: "*",
2023-05-18 14:27:28 +00:00
}
2023-05-17 14:22:31 +00:00
}
2023-05-19 15:14:58 +00:00
var w any = qq.Where
if w == nil {
w = SqlBuilder{}
2023-05-17 14:22:31 +00:00
}
2023-05-19 15:14:58 +00:00
ww, ok := w.(SqlBuilder)
if ok {
ww = append(ww, SqlBuilder{{
ship.ForeignKey, "in", "",
}}...)
2023-05-20 17:38:19 +00:00
qq.In = [][]any{ids}
2023-05-19 15:14:58 +00:00
qq.Where = ww
2023-05-18 14:27:28 +00:00
}
2023-05-19 15:14:58 +00:00
if qq.From == "" {
qq.From = ship.Table
}
}
2023-05-20 17:38:19 +00:00
err = parseRelation(isMultiple || ship.RelationType == "hasMany", db, ctx, helper.Or(isMultiple, rrs, rr), qq)
2023-05-19 15:14:58 +00:00
if err != nil && err != sql.ErrNoRows {
return err
}
2023-05-20 17:38:19 +00:00
assignment(r, helper.Or(isMultiple, rrs, rr))
2023-05-19 15:14:58 +00:00
return err
})
2023-05-17 14:22:31 +00:00
}
2023-05-18 14:27:28 +00:00
return fn, fns
2023-05-17 14:22:31 +00:00
}
2023-05-20 17:38:19 +00:00
func GetWithID[T, V any](fn func(*T) V) func(any) []any {
return func(a any) []any {
one, ok := a.(*T)
if ok {
return []any{fn(one)}
}
return slice.ToAnySlice(slice.Unique(slice.Map(*a.(*[]T), func(t T) any {
return fn(&t)
})))
}
}
func SetHasOne[T, V any, K comparable](fn func(*T, *V), idFn func(*T) K, iddFn func(*V) K) func(any, any) {
return func(m, v any) {
one, ok := m.(*T)
if ok {
fn(one, v.(*V))
return
}
r := m.(*[]T)
vv := v.(*[]V)
mm := slice.SimpleToMap(*vv, func(v V) K {
return iddFn(&v)
})
for i := 0; i < len(*r); i++ {
val := &(*r)[i]
id := idFn(val)
v, ok := mm[id]
if ok {
fn(val, &v)
}
}
}
}
func SetHasMany[T, V any, K comparable](fn func(*T, *[]V), idFn func(*T) K, iddFn func(*V) K) func(any, any) {
return func(m, v any) {
one, ok := m.(*T)
if ok {
fn(one, v.(*[]V))
return
}
r := m.(*[]T)
vv := v.(*[]V)
mm := slice.GroupBy(*vv, func(t V) (K, V) {
return iddFn(&t), t
})
for i := 0; i < len(*r); i++ {
val := &(*r)[i]
id := idFn(val)
v, ok := mm[id]
if ok {
fn(val, &v)
}
}
}
}
func RelationHasOne[M, P any, I constraints.Integer | uint64](fId func(*M) I, pId func(*P) I, setVal func(*M, *P), r Relationship) RelationFn {
return func() (func(any) []any, func(any, any), any, any, Relationship) {
var s P
var ss []P
return GetWithID(fId), SetHasOne(setVal, fId, pId), &s, &ss, r
}
}
func RelationHasMany[M, P any, I constraints.Integer | uint64](mId func(*M) I, pId func(*P) I, setVal func(*M, *[]P), r Relationship) RelationFn {
return func() (func(any) []any, func(any, any), any, any, Relationship) {
var ss []P
return GetWithID(mId), SetHasMany(setVal, mId, pId), &ss, &ss, r
}
}