Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

fix(tags): ensure JSON array elements are properly formatted in SQL queries (#4944)

Maximilian Krauß4d6042e35f0a
diff --git a/plugin/filter/common_converter.go b/plugin/filter/common_converter.go
index 098384817..47ae4314a 100644
--- a/plugin/filter/common_converter.go
+++ b/plugin/filter/common_converter.go
@@ -266,7 +266,7 @@ func (c *CommonSQLConverter) handleTagInList(ctx *ConvertContext, values []any)
template := c.dialect.GetJSONContains("$.tags", "element")
sql := strings.Replace(template, "?", c.dialect.GetParameterPlaceholder(c.paramIndex), 1)
subconditions = append(subconditions, sql)
- args = append(args, v)
+ args = append(args, fmt.Sprintf(`"%s"`, v))
}
c.paramIndex++
}
diff --git a/plugin/filter/dialect.go b/plugin/filter/dialect.go
index a6e15fa98..98ad1538c 100644
--- a/plugin/filter/dialect.go
+++ b/plugin/filter/dialect.go
@@ -185,12 +185,12 @@ func (d *PostgreSQLDialect) GetJSONArrayLength(path string) string {
func (d *PostgreSQLDialect) GetJSONContains(path, _ string) string {
jsonPath := strings.Replace(path, "$.tags", "payload->'tags'", 1)
- return fmt.Sprintf("%s.%s @> jsonb_build_array(?)", d.GetTablePrefix(), jsonPath)
+ return fmt.Sprintf("%s.%s @> jsonb_build_array(?::json)", d.GetTablePrefix(), jsonPath)
}
func (d *PostgreSQLDialect) GetJSONLike(path, _ string) string {
jsonPath := strings.Replace(path, "$.tags", "payload->'tags'", 1)
- return fmt.Sprintf("%s.%s @> jsonb_build_array(?)", d.GetTablePrefix(), jsonPath)
+ return fmt.Sprintf("%s.%s @> jsonb_build_array(?::json)", d.GetTablePrefix(), jsonPath)
}
func (*PostgreSQLDialect) GetBooleanValue(value bool) interface{} {
diff --git a/store/db/mysql/memo_filter_test.go b/store/db/mysql/memo_filter_test.go
index 330b8fa54..1c87d0a82 100644
--- a/store/db/mysql/memo_filter_test.go
+++ b/store/db/mysql/memo_filter_test.go
@@ -18,12 +18,12 @@ func TestConvertExprToSQL(t *testing.T) {
{
filter: `tag in ["tag1", "tag2"]`,
want: "(JSON_CONTAINS(JSON_EXTRACT(`memo`.`payload`, '$.tags'), ?) OR JSON_CONTAINS(JSON_EXTRACT(`memo`.`payload`, '$.tags'), ?))",
- args: []any{"tag1", "tag2"},
+ args: []any{`"tag1"`, `"tag2"`},
},
{
filter: `!(tag in ["tag1", "tag2"])`,
want: "NOT ((JSON_CONTAINS(JSON_EXTRACT(`memo`.`payload`, '$.tags'), ?) OR JSON_CONTAINS(JSON_EXTRACT(`memo`.`payload`, '$.tags'), ?)))",
- args: []any{"tag1", "tag2"},
+ args: []any{`"tag1"`, `"tag2"`},
},
{
filter: `content.contains("memos")`,
@@ -43,7 +43,7 @@ func TestConvertExprToSQL(t *testing.T) {
{
filter: `tag in ['tag1'] || content.contains('hello')`,
want: "(JSON_CONTAINS(JSON_EXTRACT(`memo`.`payload`, '$.tags'), ?) OR `memo`.`content` LIKE ?)",
- args: []any{"tag1", "%hello%"},
+ args: []any{`"tag1"`, "%hello%"},
},
{
filter: `1`,
diff --git a/store/db/postgres/memo_filter_test.go b/store/db/postgres/memo_filter_test.go
index 30f2c69e1..0ecb48ac8 100644
--- a/store/db/postgres/memo_filter_test.go
+++ b/store/db/postgres/memo_filter_test.go
@@ -17,13 +17,13 @@ func TestConvertExprToSQL(t *testing.T) {
}{
{
filter: `tag in ["tag1", "tag2"]`,
- want: "(memo.payload->'tags' @> jsonb_build_array($1) OR memo.payload->'tags' @> jsonb_build_array($2))",
- args: []any{"tag1", "tag2"},
+ want: "(memo.payload->'tags' @> jsonb_build_array($1::json) OR memo.payload->'tags' @> jsonb_build_array($2::json))",
+ args: []any{`"tag1"`, `"tag2"`},
},
{
filter: `!(tag in ["tag1", "tag2"])`,
- want: "NOT ((memo.payload->'tags' @> jsonb_build_array($1) OR memo.payload->'tags' @> jsonb_build_array($2)))",
- args: []any{"tag1", "tag2"},
+ want: "NOT ((memo.payload->'tags' @> jsonb_build_array($1::json) OR memo.payload->'tags' @> jsonb_build_array($2::json)))",
+ args: []any{`"tag1"`, `"tag2"`},
},
{
filter: `content.contains("memos")`,
@@ -42,8 +42,8 @@ func TestConvertExprToSQL(t *testing.T) {
},
{
filter: `tag in ['tag1'] || content.contains('hello')`,
- want: "(memo.payload->'tags' @> jsonb_build_array($1) OR memo.content ILIKE $2)",
- args: []any{"tag1", "%hello%"},
+ want: "(memo.payload->'tags' @> jsonb_build_array($1::json) OR memo.content ILIKE $2)",
+ args: []any{`"tag1"`, "%hello%"},
},
{
filter: `1`,
@@ -107,7 +107,7 @@ func TestConvertExprToSQL(t *testing.T) {
},
{
filter: `"work" in tags`,
- want: "memo.payload->'tags' @> jsonb_build_array($1)",
+ want: "memo.payload->'tags' @> jsonb_build_array($1::json)",
args: []any{"work"},
},
{