Compare commits
No commits in common. "b6091c6b426886af33b2550fea65f60ef0cf595a" and "b41b6e0a09440a93b59de1ca0cbb25284ee1f996" have entirely different histories.
b6091c6b42
...
b41b6e0a09
|
@ -114,9 +114,3 @@ func GetContextVal[V, K any](ctx context.Context, k K, defaults V) V {
|
||||||
}
|
}
|
||||||
return vv
|
return vv
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsImplements[T, A any](i A) (T, bool) {
|
|
||||||
var a any = i
|
|
||||||
t, ok := a.(T)
|
|
||||||
return t, ok
|
|
||||||
}
|
|
||||||
|
|
|
@ -71,23 +71,3 @@ func Unshift[T any](a *[]T, e ...T) {
|
||||||
func Push[T any](a *[]T, e ...T) {
|
func Push[T any](a *[]T, e ...T) {
|
||||||
*a = append(*a, e...)
|
*a = append(*a, e...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Decompress[T any](a [][]T) (r []T) {
|
|
||||||
for _, ts := range a {
|
|
||||||
for _, t := range ts {
|
|
||||||
r = append(r, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
func DecompressBy[T, R any](a [][]T, fn func(T) (R, bool)) (r []R) {
|
|
||||||
for _, ts := range a {
|
|
||||||
for _, t := range ts {
|
|
||||||
v, ok := fn(t)
|
|
||||||
if ok {
|
|
||||||
r = append(r, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
|
@ -22,10 +22,6 @@ type ParseWhere interface {
|
||||||
ParseWhere(*[][]any) (string, []any, error)
|
ParseWhere(*[][]any) (string, []any, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type AndWhere interface {
|
|
||||||
AndWhere(field, operator, val, fieldType string) ParseWhere
|
|
||||||
}
|
|
||||||
|
|
||||||
type dbQuery interface {
|
type dbQuery interface {
|
||||||
Select(context.Context, any, string, ...any) error
|
Select(context.Context, any, string, ...any) error
|
||||||
Get(context.Context, any, string, ...any) error
|
Get(context.Context, any, string, ...any) error
|
||||||
|
|
|
@ -186,10 +186,3 @@ func (w SqlBuilder) parseJoin() string {
|
||||||
}
|
}
|
||||||
return s.String()
|
return s.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w SqlBuilder) AndWhere(field, operator, val, fieldType string) ParseWhere {
|
|
||||||
ww := append(w, []string{
|
|
||||||
field, operator, val, fieldType,
|
|
||||||
})
|
|
||||||
return ww
|
|
||||||
}
|
|
||||||
|
|
|
@ -90,18 +90,6 @@ func parseAfterJoin(fromTable string, ids [][]any, qq *QueryCondition, ship Rela
|
||||||
tables[len(tables)-1], ship.Middle.ForeignKey), "in", ""},
|
tables[len(tables)-1], ship.Middle.ForeignKey), "in", ""},
|
||||||
)
|
)
|
||||||
qq.Where = ww
|
qq.Where = ww
|
||||||
} else {
|
|
||||||
aw, ok := helper.IsImplements[AndWhere](qq.Where)
|
|
||||||
if ok {
|
|
||||||
vv := aw.AndWhere(fmt.Sprintf("%s.%s",
|
|
||||||
tables[len(tables)-1], ship.Middle.ForeignKey), "in", strings.Join(slice.DecompressBy(ids, func(t any) (string, bool) {
|
|
||||||
return fmt.Sprintf("%v", t), true
|
|
||||||
}), ","), "int")
|
|
||||||
wa, ok := helper.IsImplements[ParseWhere](vv)
|
|
||||||
if ok {
|
|
||||||
qq.Where = wa
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if qq.Fields == "" || qq.Fields == "*" {
|
if qq.Fields == "" || qq.Fields == "*" {
|
||||||
qq.Fields = str.Join(from[len(from)-1], ".", "*", ",", tables[len(tables)-1], ".", ship.Middle.ForeignKey)
|
qq.Fields = str.Join(from[len(from)-1], ".", "*", ",", tables[len(tables)-1], ".", ship.Middle.ForeignKey)
|
||||||
|
@ -145,26 +133,18 @@ func Relation(isPlural bool, db dbQuery, ctx context.Context, r any, q *QueryCon
|
||||||
if w == nil {
|
if w == nil {
|
||||||
qq.Where = SqlBuilder{}
|
qq.Where = SqlBuilder{}
|
||||||
}
|
}
|
||||||
|
ww, ok := qq.Where.(SqlBuilder)
|
||||||
in := [][]any{ids}
|
in := [][]any{ids}
|
||||||
if ship.Middle != nil {
|
if ok {
|
||||||
isPlural = parseAfterJoin(qq.From, in, qq, ship)
|
if ship.Middle != nil {
|
||||||
} else {
|
isPlural = parseAfterJoin(qq.From, in, qq, ship)
|
||||||
ww, ok := qq.Where.(SqlBuilder)
|
} else {
|
||||||
if ok {
|
|
||||||
ww = append(ww, SqlBuilder{{
|
ww = append(ww, SqlBuilder{{
|
||||||
ship.ForeignKey, "in", "",
|
ship.ForeignKey, "in", "",
|
||||||
}}...)
|
}}...)
|
||||||
|
qq.In = in
|
||||||
qq.Where = ww
|
qq.Where = ww
|
||||||
} else {
|
|
||||||
aw, ok := helper.IsImplements[AndWhere](qq.Where)
|
|
||||||
if ok {
|
|
||||||
ww := aw.AndWhere(ship.ForeignKey, "in", strings.Join(slice.Map(ids, func(t any) string {
|
|
||||||
return fmt.Sprintf("%v", t)
|
|
||||||
}), ","), "int")
|
|
||||||
qq.Where = ww
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
qq.In = in
|
|
||||||
}
|
}
|
||||||
err = ParseRelation(isPlural || ship.RelationType == HasMany, db, ctx, helper.Or(isPlural, rrs, rr), qq)
|
err = ParseRelation(isPlural || ship.RelationType == HasMany, db, ctx, helper.Or(isPlural, rrs, rr), qq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user