feat: update slice expr

This commit is contained in:
Rogee
2025-05-26 14:31:41 +08:00
parent d634e0175b
commit 0a8f6f9a1b
5 changed files with 39 additions and 13 deletions

View File

@@ -67,14 +67,10 @@ func (m *Medias) GetByIds(ctx context.Context, ids []int64) ([]*Medias, error) {
return nil, nil return nil, nil
} }
condIds := lo.Map(ids, func(id int64, _ int) Expression {
return Int64(id)
})
tbl := tblMedias tbl := tblMedias
stmt := tbl. stmt := tbl.
SELECT(tbl.AllColumns). SELECT(tbl.AllColumns).
WHERE(tbl.ID.IN(condIds...)) WHERE(tbl.ID.IN(IntExprSlice(ids)...))
m.log().Infof("sql: %s", stmt.DebugSql()) m.log().Infof("sql: %s", stmt.DebugSql())
var medias []Medias var medias []Medias

View File

@@ -84,9 +84,7 @@ func (m *Orders) List(
return nil, err return nil, err
} }
postsMap, err := PostsModel().GetPostsMapByIDs(ctx, lo.Map(orders, func(order Orders, _ int) int64 { postsMap, err := PostsModel().GetPostsMapByIDs(ctx, lo.Map(orders, func(order Orders, _ int) int64 { return order.PostID }))
return order.PostID
}))
if err != nil { if err != nil {
m.log().Errorf("error getting posts map: %v", err) m.log().Errorf("error getting posts map: %v", err)
return nil, err return nil, err

View File

@@ -125,7 +125,7 @@ func (m *Posts) BoughtStatistics(ctx context.Context, postIds []int64) (map[int6
tbl.PostID.AS("post_id"), tbl.PostID.AS("post_id"),
). ).
WHERE( WHERE(
tbl.PostID.IN(lo.Map(postIds, func(id int64, _ int) Expression { return Int64(id) })...), tbl.PostID.IN(IntExprSlice(postIds)...),
). ).
GROUP_BY( GROUP_BY(
tbl.PostID, tbl.PostID,
@@ -218,7 +218,7 @@ func (m *Posts) GetPostsMapByIDs(ctx context.Context, ids []int64) (map[int64]Po
stmt := tbl. stmt := tbl.
SELECT(tbl.AllColumns). SELECT(tbl.AllColumns).
WHERE( 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()) m.log().Infof("sql: %s", stmt.DebugSql())
@@ -245,7 +245,7 @@ func (m *Posts) GetMediaByIds(ctx context.Context, ids []int64) ([]Medias, error
stmt := tbl. stmt := tbl.
SELECT(tbl.AllColumns). SELECT(tbl.AllColumns).
WHERE( 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()) m.log().Infof("sql: %s", stmt.DebugSql())

View File

@@ -10,10 +10,12 @@ import (
"quyun/database/table" "quyun/database/table"
. "github.com/go-jet/jet/v2/postgres" . "github.com/go-jet/jet/v2/postgres"
"github.com/samber/lo"
"go.ipao.vip/atom" "go.ipao.vip/atom"
"go.ipao.vip/atom/container" "go.ipao.vip/atom/container"
"go.ipao.vip/atom/contracts" "go.ipao.vip/atom/contracts"
"go.ipao.vip/atom/opt" "go.ipao.vip/atom/opt"
"golang.org/x/exp/constraints"
) )
type Cond func(BoolExpression) BoolExpression type Cond func(BoolExpression) BoolExpression
@@ -36,6 +38,36 @@ func CondJoin(cond Cond, conds ...Cond) []Cond {
return append([]Cond{cond}, conds...) 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 // tables
var tblMedias = table.Medias var tblMedias = table.Medias
var tblOrders = table.Orders var tblOrders = table.Orders

View File

@@ -210,7 +210,7 @@ func (m *Users) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int64]Us
stmt := tbl. stmt := tbl.
SELECT(tbl.AllColumns). SELECT(tbl.AllColumns).
WHERE( 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()) m.log().Infof("sql: %s", stmt.DebugSql())
@@ -231,7 +231,7 @@ func (m *Users) BatchCheckHasBought(ctx context.Context, postIDs []int64) (map[i
tbl := tblUserPosts tbl := tblUserPosts
stmt := tbl.SELECT(tbl.PostID.AS("post_id")).WHERE( stmt := tbl.SELECT(tbl.PostID.AS("post_id")).WHERE(
tbl.UserID.EQ(Int64(m.ID)).AND( 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)...),
), ),
) )