From 18b9b9d18f55cd9dc7e4bed607a3d3bc601f200b Mon Sep 17 00:00:00 2001 From: Johnny Date: Sat, 12 Apr 2025 22:13:18 +0800 Subject: [PATCH] chore: implement memo filter in list memo relations --- store/db/mysql/memo_relation.go | 19 +++++++++++++++++++ store/db/postgres/memo_relation.go | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/store/db/mysql/memo_relation.go b/store/db/mysql/memo_relation.go index b75c9b7c..d30fb926 100644 --- a/store/db/mysql/memo_relation.go +++ b/store/db/mysql/memo_relation.go @@ -40,6 +40,25 @@ func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation if find.Type != nil { where, args = append(where, "`type` = ?"), append(args, find.Type) } + if find.MemoFilter != nil { + // Parse filter string and return the parsed expression. + // The filter string should be a CEL expression. + parsedExpr, err := filter.Parse(*find.MemoFilter, filter.MemoFilterCELAttributes...) + if err != nil { + return nil, err + } + convertCtx := filter.NewConvertContext() + // ConvertExprToSQL converts the parsed expression to a SQL condition string. + if err := d.ConvertExprToSQL(convertCtx, parsedExpr.GetExpr()); err != nil { + return nil, err + } + condition := convertCtx.Buffer.String() + if condition != "" { + where = append(where, fmt.Sprintf("memo_id IN (SELECT id FROM memo WHERE %s)", condition)) + where = append(where, fmt.Sprintf("related_memo_id IN (SELECT id FROM memo WHERE %s)", condition)) + args = append(args, append(convertCtx.Args, convertCtx.Args...)...) + } + } rows, err := d.db.QueryContext(ctx, "SELECT `memo_id`, `related_memo_id`, `type` FROM `memo_relation` WHERE "+strings.Join(where, " AND "), args...) if err != nil { diff --git a/store/db/postgres/memo_relation.go b/store/db/postgres/memo_relation.go index 5b406035..4d12b771 100644 --- a/store/db/postgres/memo_relation.go +++ b/store/db/postgres/memo_relation.go @@ -46,6 +46,25 @@ func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation if find.Type != nil { where, args = append(where, "type = "+placeholder(len(args)+1)), append(args, find.Type) } + if find.MemoFilter != nil { + // Parse filter string and return the parsed expression. + // The filter string should be a CEL expression. + parsedExpr, err := filter.Parse(*find.MemoFilter, filter.MemoFilterCELAttributes...) + if err != nil { + return nil, err + } + convertCtx := filter.NewConvertContext() + // ConvertExprToSQL converts the parsed expression to a SQL condition string. + if err := d.ConvertExprToSQL(convertCtx, parsedExpr.GetExpr()); err != nil { + return nil, err + } + condition := convertCtx.Buffer.String() + if condition != "" { + where = append(where, fmt.Sprintf("memo_id IN (SELECT id FROM memo WHERE %s)", condition)) + where = append(where, fmt.Sprintf("related_memo_id IN (SELECT id FROM memo WHERE %s)", condition)) + args = append(args, append(convertCtx.Args, convertCtx.Args...)...) + } + } rows, err := d.db.QueryContext(ctx, ` SELECT