chore: implement memo filter in list memo relations

This commit is contained in:
Johnny
2025-04-12 22:13:18 +08:00
committed by GitHub
parent 08f9b18ced
commit 18b9b9d18f
2 changed files with 38 additions and 0 deletions

View File

@@ -40,6 +40,25 @@ func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation
if find.Type != nil { if find.Type != nil {
where, args = append(where, "`type` = ?"), append(args, find.Type) 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...) 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 { if err != nil {

View File

@@ -46,6 +46,25 @@ func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation
if find.Type != nil { if find.Type != nil {
where, args = append(where, "type = "+placeholder(len(args)+1)), append(args, find.Type) 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, ` rows, err := d.db.QueryContext(ctx, `
SELECT SELECT