mirror of
https://github.com/usememos/memos.git
synced 2025-12-17 14:19:17 +08:00
fix(postgres): update tag filtering SQL to ensure proper type casting for LIKE comparisons
This commit is contained in:
parent
50f49fc00d
commit
07a030ddfd
2 changed files with 4 additions and 4 deletions
|
|
@ -352,7 +352,7 @@ func (r *renderer) renderTagInList(values []ValueExpr) (renderResult, error) {
|
||||||
case DialectPostgres:
|
case DialectPostgres:
|
||||||
// Support hierarchical tags: match exact tag OR tags with this prefix
|
// Support hierarchical tags: match exact tag OR tags with this prefix
|
||||||
exactMatch := fmt.Sprintf("%s @> jsonb_build_array(%s::json)", jsonArrayExpr(r.dialect, field), r.addArg(fmt.Sprintf(`"%s"`, str)))
|
exactMatch := fmt.Sprintf("%s @> jsonb_build_array(%s::json)", jsonArrayExpr(r.dialect, field), r.addArg(fmt.Sprintf(`"%s"`, str)))
|
||||||
prefixMatch := fmt.Sprintf("%s::text LIKE %s::text", jsonArrayExpr(r.dialect, field), r.addArg(fmt.Sprintf(`%%"%s/%%`, str)))
|
prefixMatch := fmt.Sprintf("(%s)::text LIKE %s", jsonArrayExpr(r.dialect, field), r.addArg(fmt.Sprintf(`%%"%s/%%`, str)))
|
||||||
expr := fmt.Sprintf("(%s OR %s)", exactMatch, prefixMatch)
|
expr := fmt.Sprintf("(%s OR %s)", exactMatch, prefixMatch)
|
||||||
conditions = append(conditions, expr)
|
conditions = append(conditions, expr)
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,12 @@ func TestConvertExprToSQL(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
filter: `tag in ["tag1", "tag2"]`,
|
filter: `tag in ["tag1", "tag2"]`,
|
||||||
want: "((memo.payload->'tags' @> jsonb_build_array($1::json) OR memo.payload->'tags'::text LIKE $2::text) OR (memo.payload->'tags' @> jsonb_build_array($3::json) OR memo.payload->'tags'::text LIKE $4::text))",
|
want: "((memo.payload->'tags' @> jsonb_build_array($1::json) OR (memo.payload->'tags')::text LIKE $2) OR (memo.payload->'tags' @> jsonb_build_array($3::json) OR (memo.payload->'tags')::text LIKE $4))",
|
||||||
args: []any{`"tag1"`, `%"tag1/%`, `"tag2"`, `%"tag2/%`},
|
args: []any{`"tag1"`, `%"tag1/%`, `"tag2"`, `%"tag2/%`},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
filter: `!(tag in ["tag1", "tag2"])`,
|
filter: `!(tag in ["tag1", "tag2"])`,
|
||||||
want: "NOT (((memo.payload->'tags' @> jsonb_build_array($1::json) OR memo.payload->'tags'::text LIKE $2::text) OR (memo.payload->'tags' @> jsonb_build_array($3::json) OR memo.payload->'tags'::text LIKE $4::text)))",
|
want: "NOT (((memo.payload->'tags' @> jsonb_build_array($1::json) OR (memo.payload->'tags')::text LIKE $2) OR (memo.payload->'tags' @> jsonb_build_array($3::json) OR (memo.payload->'tags')::text LIKE $4)))",
|
||||||
args: []any{`"tag1"`, `%"tag1/%`, `"tag2"`, `%"tag2/%`},
|
args: []any{`"tag1"`, `%"tag1/%`, `"tag2"`, `%"tag2/%`},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -43,7 +43,7 @@ func TestConvertExprToSQL(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
filter: `tag in ['tag1'] || content.contains('hello')`,
|
filter: `tag in ['tag1'] || content.contains('hello')`,
|
||||||
want: "((memo.payload->'tags' @> jsonb_build_array($1::json) OR memo.payload->'tags'::text LIKE $2::text) OR memo.content ILIKE $3)",
|
want: "((memo.payload->'tags' @> jsonb_build_array($1::json) OR (memo.payload->'tags')::text LIKE $2) OR memo.content ILIKE $3)",
|
||||||
args: []any{`"tag1"`, `%"tag1/%`, "%hello%"},
|
args: []any{`"tag1"`, `%"tag1/%`, "%hello%"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue