Skip to content

Commit 18b9b9d

Browse files
authored
chore: implement memo filter in list memo relations
1 parent 08f9b18 commit 18b9b9d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

store/db/mysql/memo_relation.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation
4040
if find.Type != nil {
4141
where, args = append(where, "`type` = ?"), append(args, find.Type)
4242
}
43+
if find.MemoFilter != nil {
44+
// Parse filter string and return the parsed expression.
45+
// The filter string should be a CEL expression.
46+
parsedExpr, err := filter.Parse(*find.MemoFilter, filter.MemoFilterCELAttributes...)
47+
if err != nil {
48+
return nil, err
49+
}
50+
convertCtx := filter.NewConvertContext()
51+
// ConvertExprToSQL converts the parsed expression to a SQL condition string.
52+
if err := d.ConvertExprToSQL(convertCtx, parsedExpr.GetExpr()); err != nil {
53+
return nil, err
54+
}
55+
condition := convertCtx.Buffer.String()
56+
if condition != "" {
57+
where = append(where, fmt.Sprintf("memo_id IN (SELECT id FROM memo WHERE %s)", condition))
58+
where = append(where, fmt.Sprintf("related_memo_id IN (SELECT id FROM memo WHERE %s)", condition))
59+
args = append(args, append(convertCtx.Args, convertCtx.Args...)...)
60+
}
61+
}
4362

4463
rows, err := d.db.QueryContext(ctx, "SELECT `memo_id`, `related_memo_id`, `type` FROM `memo_relation` WHERE "+strings.Join(where, " AND "), args...)
4564
if err != nil {

store/db/postgres/memo_relation.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation
4646
if find.Type != nil {
4747
where, args = append(where, "type = "+placeholder(len(args)+1)), append(args, find.Type)
4848
}
49+
if find.MemoFilter != nil {
50+
// Parse filter string and return the parsed expression.
51+
// The filter string should be a CEL expression.
52+
parsedExpr, err := filter.Parse(*find.MemoFilter, filter.MemoFilterCELAttributes...)
53+
if err != nil {
54+
return nil, err
55+
}
56+
convertCtx := filter.NewConvertContext()
57+
// ConvertExprToSQL converts the parsed expression to a SQL condition string.
58+
if err := d.ConvertExprToSQL(convertCtx, parsedExpr.GetExpr()); err != nil {
59+
return nil, err
60+
}
61+
condition := convertCtx.Buffer.String()
62+
if condition != "" {
63+
where = append(where, fmt.Sprintf("memo_id IN (SELECT id FROM memo WHERE %s)", condition))
64+
where = append(where, fmt.Sprintf("related_memo_id IN (SELECT id FROM memo WHERE %s)", condition))
65+
args = append(args, append(convertCtx.Args, convertCtx.Args...)...)
66+
}
67+
}
4968

5069
rows, err := d.db.QueryContext(ctx, `
5170
SELECT

0 commit comments

Comments
 (0)