mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
fix: filter
This commit is contained in:
@@ -113,7 +113,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
where = append(where, condition)
|
where = append(where, fmt.Sprintf("(%s)", condition))
|
||||||
}
|
}
|
||||||
if find.ExcludeComments {
|
if find.ExcludeComments {
|
||||||
where = append(where, "`parent_id` IS NULL")
|
where = append(where, "`parent_id` IS NULL")
|
||||||
|
@@ -18,21 +18,22 @@ func RestoreExprToSQL(expr *exprv1.Expr) (string, error) {
|
|||||||
case *exprv1.Expr_CallExpr:
|
case *exprv1.Expr_CallExpr:
|
||||||
switch v.CallExpr.Function {
|
switch v.CallExpr.Function {
|
||||||
case "_||_", "_&&_":
|
case "_||_", "_&&_":
|
||||||
for _, arg := range v.CallExpr.Args {
|
if len(v.CallExpr.Args) != 2 {
|
||||||
left, err := RestoreExprToSQL(arg)
|
return "", errors.Errorf("invalid number of arguments for %s", v.CallExpr.Function)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
right, err := RestoreExprToSQL(arg)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
operator := "AND"
|
|
||||||
if v.CallExpr.Function == "_||_" {
|
|
||||||
operator = "OR"
|
|
||||||
}
|
|
||||||
condition = fmt.Sprintf("(%s %s %s)", left, operator, right)
|
|
||||||
}
|
}
|
||||||
|
left, err := RestoreExprToSQL(v.CallExpr.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
right, err := RestoreExprToSQL(v.CallExpr.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
operator := "AND"
|
||||||
|
if v.CallExpr.Function == "_||_" {
|
||||||
|
operator = "OR"
|
||||||
|
}
|
||||||
|
condition = fmt.Sprintf("(%s %s %s)", left, operator, right)
|
||||||
case "_==_", "_!=_", "_<_", "_>_", "_<=_", "_>=_":
|
case "_==_", "_!=_", "_<_", "_>_", "_<=_", "_>=_":
|
||||||
if len(v.CallExpr.Args) != 2 {
|
if len(v.CallExpr.Args) != 2 {
|
||||||
return "", errors.Errorf("invalid number of arguments for %s", v.CallExpr.Function)
|
return "", errors.Errorf("invalid number of arguments for %s", v.CallExpr.Function)
|
||||||
@@ -130,7 +131,7 @@ func RestoreExprToSQL(expr *exprv1.Expr) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
condition = fmt.Sprintf("`memo`.`content` LIKE %s", fmt.Sprintf(`%%"%s"%%`, arg))
|
condition = fmt.Sprintf("`memo`.`content` LIKE %s", fmt.Sprintf(`'%%%s%%'`, arg))
|
||||||
case "!_":
|
case "!_":
|
||||||
if len(v.CallExpr.Args) != 1 {
|
if len(v.CallExpr.Args) != 1 {
|
||||||
return "", errors.Errorf("invalid number of arguments for %s", v.CallExpr.Function)
|
return "", errors.Errorf("invalid number of arguments for %s", v.CallExpr.Function)
|
||||||
|
@@ -23,11 +23,11 @@ func TestRestoreExprToSQL(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
filter: `tag in ["tag1", "tag2"] || tag in ["tag3", "tag4"]`,
|
filter: `tag in ["tag1", "tag2"] || tag in ["tag3", "tag4"]`,
|
||||||
want: "((JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE '%\"tag3\"%' OR JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE '%\"tag4\"%') OR (JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE '%\"tag3\"%' OR JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE '%\"tag4\"%'))",
|
want: "((JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE '%\"tag1\"%' OR JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE '%\"tag2\"%') OR (JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE '%\"tag3\"%' OR JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE '%\"tag4\"%'))",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
filter: `content.contains("memos")`,
|
filter: `content.contains("memos")`,
|
||||||
want: "`memo`.`content` LIKE %\"memos\"%",
|
want: "`memo`.`content` LIKE '%memos%'",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
filter: `visibility in ["PUBLIC"]`,
|
filter: `visibility in ["PUBLIC"]`,
|
||||||
@@ -41,6 +41,10 @@ func TestRestoreExprToSQL(t *testing.T) {
|
|||||||
filter: `create_time == "2006-01-02T15:04:05+07:00"`,
|
filter: `create_time == "2006-01-02T15:04:05+07:00"`,
|
||||||
want: "`memo`.`created_ts` = 1136189045",
|
want: "`memo`.`created_ts` = 1136189045",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
filter: `tag in ['tag1'] || content.contains('hello')`,
|
||||||
|
want: "(JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE '%\"tag1\"%' OR `memo`.`content` LIKE '%hello%')",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
Reference in New Issue
Block a user