diff --git a/pkg/db/sqlite/search.go b/pkg/db/sqlite/search.go index e1ca4f1..0eb4cdd 100644 --- a/pkg/db/sqlite/search.go +++ b/pkg/db/sqlite/search.go @@ -3,6 +3,7 @@ package sqlite import ( "errors" "fmt" + "sort" sq "github.com/Masterminds/squirrel" "github.com/dstotijn/hetty/pkg/search" @@ -32,6 +33,8 @@ func parseSearchExpr(expr search.Expression) (sq.Sqlizer, error) { return parsePrefixExpr(e) case *search.InfixExpression: return parseInfixExpr(e) + case *search.StringLiteral: + return parseStringLiteral(e) default: return nil, fmt.Errorf("expression type (%v) not supported", expr) } @@ -106,3 +109,18 @@ func parseInfixExpr(expr *search.InfixExpression) (sq.Sqlizer, error) { return nil, errors.New("unsupported operator") } } + +func parseStringLiteral(strLiteral *search.StringLiteral) (sq.Sqlizer, error) { + // Sorting is not necessary, but makes it easier to do assertions in tests. + sortedKeys := make([]string, 0, len(stringLiteralMap)) + for _, v := range stringLiteralMap { + sortedKeys = append(sortedKeys, v) + } + sort.Strings(sortedKeys) + + or := make(sq.Or, len(stringLiteralMap)) + for i, value := range sortedKeys { + or[i] = sq.Like{value: "%" + strLiteral.Value + "%"} + } + return or, nil +} diff --git a/pkg/db/sqlite/search_test.go b/pkg/db/sqlite/search_test.go index 1770b0c..15f5169 100644 --- a/pkg/db/sqlite/search_test.go +++ b/pkg/db/sqlite/search_test.go @@ -170,6 +170,26 @@ func TestParseSearchExpr(t *testing.T) { }, expectedError: nil, }, + { + name: "foo", + searchExpr: &search.StringLiteral{ + Value: "foo", + }, + expectedSqlizer: sq.Or{ + sq.Like{"req.body": "%foo%"}, + sq.Like{"req.id": "%foo%"}, + sq.Like{"req.method": "%foo%"}, + sq.Like{"req.proto": "%foo%"}, + sq.Like{"req.timestamp": "%foo%"}, + sq.Like{"req.url": "%foo%"}, + sq.Like{"res.body": "%foo%"}, + sq.Like{"res.id": "%foo%"}, + sq.Like{"res.proto": "%foo%"}, + sq.Like{"res.status_code": "%foo%"}, + sq.Like{"res.status_reason": "%foo%"}, + sq.Like{"res.timestamp": "%foo%"}, + }, + }, } for _, tt := range tests {