From 07336f0392ca97aed048365e34c472c486a911e8 Mon Sep 17 00:00:00 2001 From: johnnyjoy Date: Wed, 5 Feb 2025 20:07:38 +0800 Subject: [PATCH] chore: update condition concat check --- store/db/mysql/memo.go | 7 +++++-- store/db/postgres/memo.go | 7 +++++-- store/db/sqlite/memo.go | 7 +++++-- store/db/sqlite/memo_filter_test.go | 5 +++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/store/db/mysql/memo.go b/store/db/mysql/memo.go index 5b1b46670..271fe0c9d 100644 --- a/store/db/mysql/memo.go +++ b/store/db/mysql/memo.go @@ -121,8 +121,11 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo if err := d.ConvertExprToSQL(convertCtx, parsedExpr.GetExpr()); err != nil { return nil, err } - where = append(where, fmt.Sprintf("(%s)", convertCtx.Buffer.String())) - args = append(args, convertCtx.Args...) + condition := convertCtx.Buffer.String() + if condition != "" { + where = append(where, fmt.Sprintf("(%s)", condition)) + args = append(args, convertCtx.Args...) + } } if find.ExcludeComments { having = append(having, "`parent_id` IS NULL") diff --git a/store/db/postgres/memo.go b/store/db/postgres/memo.go index 242fe0409..104c8a04c 100644 --- a/store/db/postgres/memo.go +++ b/store/db/postgres/memo.go @@ -113,8 +113,11 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo if err := d.ConvertExprToSQL(convertCtx, parsedExpr.GetExpr()); err != nil { return nil, err } - where = append(where, fmt.Sprintf("(%s)", convertCtx.Buffer.String())) - args = append(args, convertCtx.Args...) + condition := convertCtx.Buffer.String() + if condition != "" { + where = append(where, fmt.Sprintf("(%s)", condition)) + args = append(args, convertCtx.Args...) + } } if find.ExcludeComments { where = append(where, "memo_relation.related_memo_id IS NULL") diff --git a/store/db/sqlite/memo.go b/store/db/sqlite/memo.go index d1ea204ad..a66d7c00c 100644 --- a/store/db/sqlite/memo.go +++ b/store/db/sqlite/memo.go @@ -113,8 +113,11 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo if err := d.ConvertExprToSQL(convertCtx, parsedExpr.GetExpr()); err != nil { return nil, err } - where = append(where, fmt.Sprintf("(%s)", convertCtx.Buffer.String())) - args = append(args, convertCtx.Args...) + condition := convertCtx.Buffer.String() + if condition != "" { + where = append(where, fmt.Sprintf("(%s)", condition)) + args = append(args, convertCtx.Args...) + } } if find.ExcludeComments { where = append(where, "`parent_id` IS NULL") diff --git a/store/db/sqlite/memo_filter_test.go b/store/db/sqlite/memo_filter_test.go index c78b5d0dd..27889ec7f 100644 --- a/store/db/sqlite/memo_filter_test.go +++ b/store/db/sqlite/memo_filter_test.go @@ -54,6 +54,11 @@ func TestConvertExprToSQL(t *testing.T) { want: "(JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE ? OR `memo`.`content` LIKE ?)", args: []any{`%"tag1"%`, "%hello%"}, }, + { + filter: `1`, + want: "", + args: []any{}, + }, } for _, tt := range tests {