Skip to content

Commit

Permalink
Add support for string literal expressions in sqlite pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
dstotijn committed Dec 24, 2020
1 parent 194d727 commit 8c2efdb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/db/sqlite/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlite
import (
"errors"
"fmt"
"sort"

sq "github.com/Masterminds/squirrel"
"github.com/dstotijn/hetty/pkg/search"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions pkg/db/sqlite/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8c2efdb

Please sign in to comment.