feat: update slice expr
This commit is contained in:
@@ -67,14 +67,10 @@ func (m *Medias) GetByIds(ctx context.Context, ids []int64) ([]*Medias, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
||||
return Int64(id)
|
||||
})
|
||||
|
||||
tbl := tblMedias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(tbl.ID.IN(condIds...))
|
||||
WHERE(tbl.ID.IN(IntExprSlice(ids)...))
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var medias []Medias
|
||||
|
||||
@@ -84,9 +84,7 @@ func (m *Orders) List(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
postsMap, err := PostsModel().GetPostsMapByIDs(ctx, lo.Map(orders, func(order Orders, _ int) int64 {
|
||||
return order.PostID
|
||||
}))
|
||||
postsMap, err := PostsModel().GetPostsMapByIDs(ctx, lo.Map(orders, func(order Orders, _ int) int64 { return order.PostID }))
|
||||
if err != nil {
|
||||
m.log().Errorf("error getting posts map: %v", err)
|
||||
return nil, err
|
||||
|
||||
@@ -125,7 +125,7 @@ func (m *Posts) BoughtStatistics(ctx context.Context, postIds []int64) (map[int6
|
||||
tbl.PostID.AS("post_id"),
|
||||
).
|
||||
WHERE(
|
||||
tbl.PostID.IN(lo.Map(postIds, func(id int64, _ int) Expression { return Int64(id) })...),
|
||||
tbl.PostID.IN(IntExprSlice(postIds)...),
|
||||
).
|
||||
GROUP_BY(
|
||||
tbl.PostID,
|
||||
@@ -218,7 +218,7 @@ func (m *Posts) GetPostsMapByIDs(ctx context.Context, ids []int64) (map[int64]Po
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.ID.IN(lo.Map(ids, func(id int64, _ int) Expression { return Int64(id) })...),
|
||||
tbl.ID.IN(IntExprSlice(ids)...),
|
||||
)
|
||||
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
@@ -245,7 +245,7 @@ func (m *Posts) GetMediaByIds(ctx context.Context, ids []int64) ([]Medias, error
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.ID.IN(lo.Map(ids, func(id int64, _ int) Expression { return Int64(id) })...),
|
||||
tbl.ID.IN(IntExprSlice(ids)...),
|
||||
)
|
||||
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
@@ -10,10 +10,12 @@ import (
|
||||
"quyun/database/table"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/samber/lo"
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/atom/opt"
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
type Cond func(BoolExpression) BoolExpression
|
||||
@@ -36,6 +38,36 @@ func CondJoin(cond Cond, conds ...Cond) []Cond {
|
||||
return append([]Cond{cond}, conds...)
|
||||
}
|
||||
|
||||
// converts
|
||||
func IntExprSlice[T constraints.Integer](slice []T) []Expression {
|
||||
if len(slice) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return lo.Map(slice, func(item T, _ int) Expression {
|
||||
switch any(item).(type) {
|
||||
case int8:
|
||||
return Int8(int8(item))
|
||||
case int16:
|
||||
return Int16(int16(item))
|
||||
case int32:
|
||||
return Int32(int32(item))
|
||||
case int64:
|
||||
return Int64(int64(item))
|
||||
case uint8:
|
||||
return Uint8(uint8(item))
|
||||
case uint16:
|
||||
return Uint16(uint16(item))
|
||||
case uint32:
|
||||
return Uint32(uint32(item))
|
||||
case uint64:
|
||||
return Uint64(uint64(item))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// tables
|
||||
var tblMedias = table.Medias
|
||||
var tblOrders = table.Orders
|
||||
|
||||
@@ -210,7 +210,7 @@ func (m *Users) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int64]Us
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.ID.IN(lo.Map(ids, func(id int64, _ int) Expression { return Int64(id) })...),
|
||||
tbl.ID.IN(IntExprSlice(ids)...),
|
||||
)
|
||||
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
@@ -231,7 +231,7 @@ func (m *Users) BatchCheckHasBought(ctx context.Context, postIDs []int64) (map[i
|
||||
tbl := tblUserPosts
|
||||
stmt := tbl.SELECT(tbl.PostID.AS("post_id")).WHERE(
|
||||
tbl.UserID.EQ(Int64(m.ID)).AND(
|
||||
tbl.PostID.IN(lo.Map(postIDs, func(id int64, _ int) Expression { return Int64(id) })...),
|
||||
tbl.PostID.IN(IntExprSlice(postIDs)...),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user