Skip to content
This repository has been archived by the owner on Feb 11, 2025. It is now read-only.

Commit

Permalink
Add tilde (like) operator (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomakado authored Feb 10, 2025
1 parent 9158624 commit 4663ece
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 57 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ period_months < 4

### Field expression operators

| Operator | Meaning | Supported types |
|----------------------|---------------|------------------------------|
| `:` or `=` | Equal, one of | `int64`, `float64`, `string` |
| `!=` or `!:` | Not equal | `int64`, `float64`, `string` |
| `>`, `>=`, `<`, `<=` | Comparison | `int64`, `float64` |
| Operator | Meaning | Supported types |
|----------------------|-------------------------------|------------------------------|
| `:` or `=` | Equal, one of | `int64`, `float64`, `string` |
| `!=` or `!:` | Not equal | `int64`, `float64`, `string` |
| `~` | “Like” or “contains” operator | `string` |
| `>`, `>=`, `<`, `<=` | Comparison | `int64`, `float64` |


### Boolean operators
Expand Down
13 changes: 7 additions & 6 deletions dumbql.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
// SingleCharEscape <- ["\\/bfnrt]
// UnicodeEscape <- 'u' HexDigit HexDigit HexDigit HexDigit
// HexDigit <- [0-9a-f]i
// CmpOp <- ( ">=" / ">" / "<=" / "<" / "!:" / "!=" / ":" / "=" )
// CmpOp <- ( ">=" / ">" / "<=" / "<" / "!:" / "!=" / ":" / "=" / "~" )
// OneOfExpr <- '[' _ values:(OneOfValues)? _ ']'
// OneOfValues <- head:OneOfValue tail:(_ ',' _ OneOfValue)*
// _ <- [ \t\r\n]*
Expand All @@ -58,11 +58,12 @@
//
// # Field expression operators
//
// | Operator | Meaning | Supported types |
// |----------------------|---------------|------------------------------|
// | `:` or `=` | Equal, one of | `int64`, `float64`, `string` |
// | `!=` or `!:` | Not equal | `int64`, `float64`, `string` |
// | `>`, `>=`, `<`, `<=` | Comparison | `int64`, `float64` |
// | Operator | Meaning | Supported types |
// |----------------------|-------------------------------|------------------------------|
// | `:` or `=` | Equal, one of | `int64`, `float64`, `string` |
// | `!=` or `!:` | Not equal | `int64`, `float64`, `string` |
// | `~` | “Like” or “contains” operator | `string` |
// | `>`, `>=`, `<`, `<=` | Comparison | `int64`, `float64` |
//
// # Boolean operators
//
Expand Down
3 changes: 3 additions & 0 deletions query/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const (
GreaterThanOrEqual
LessThan
LessThanOrEqual
Like
)

func (c FieldOperator) String() string {
Expand All @@ -138,6 +139,8 @@ func (c FieldOperator) String() string {
return "<"
case LessThanOrEqual:
return "<="
case Like:
return "~"
default:
return "unknown!"
}
Expand Down
2 changes: 1 addition & 1 deletion query/grammar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ EscapeSequence <- SingleCharEscape / UnicodeEscape
SingleCharEscape <- ["\\/bfnrt]
UnicodeEscape <- 'u' HexDigit HexDigit HexDigit HexDigit
HexDigit <- [0-9a-f]i
CmpOp <- ( ">=" / ">" / "<=" / "<" / "!:" / "!=" / ":" / "=" )
CmpOp <- ( ">=" / ">" / "<=" / "<" / "!:" / "!=" / ":" / "=" / "~" )
OneOfExpr <- '[' _ values:(OneOfValues)? _ ']' { return parseOneOfExpression(values) }
OneOfValues <- head:OneOfValue tail:(_ ',' _ OneOfValue)* { return parseOneOfValues(head, tail) }
_ <- [ \t\r\n]*
90 changes: 45 additions & 45 deletions query/parser.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions query/parser_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func resolveFieldOperator(op any) (FieldOperator, error) {
return NotEqual, nil
case ":", "=":
return Equal, nil
case "~":
return Like, nil
default:
return 0, fmt.Errorf("unknown compare operator %q", op)
}
Expand Down
2 changes: 2 additions & 0 deletions query/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func (f *FieldExpr) ToSql() (string, []any, error) { //nolint:revive
sqlizer = sq.Lt{field: value}
case LessThanOrEqual:
sqlizer = sq.LtOrEq{field: value}
case Like:
sqlizer = sq.Like{field: value}
default:
return "", nil, fmt.Errorf("unknown operator %q", f.Op)
}
Expand Down
7 changes: 7 additions & 0 deletions query/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func TestToSql(t *testing.T) { //nolint:funlen
want: "SELECT * FROM dummy_table WHERE NOT (status = ? AND eps < ?)",
wantArgs: []any{int64(200), 0.003},
},
{
input: `name~"John"`,
want: "SELECT * FROM dummy_table WHERE name LIKE ?",
wantArgs: []any{
"John",
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit 4663ece

Please sign in to comment.