mirror of
https://github.com/dstotijn/hetty
synced 2024-11-22 19:53:04 +00:00
Add support for string literal expressions in sqlite pkg
This commit is contained in:
parent
194d727f4f
commit
8c2efdb285
2 changed files with 38 additions and 0 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue