mirror of
https://github.com/dstotijn/hetty.git
synced 2025-07-01 18:47:29 -04:00
Add support for string literal expressions in sqlite pkg
This commit is contained in:
@ -3,6 +3,7 @@ package sqlite
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
|
||||||
sq "github.com/Masterminds/squirrel"
|
sq "github.com/Masterminds/squirrel"
|
||||||
"github.com/dstotijn/hetty/pkg/search"
|
"github.com/dstotijn/hetty/pkg/search"
|
||||||
@ -32,6 +33,8 @@ func parseSearchExpr(expr search.Expression) (sq.Sqlizer, error) {
|
|||||||
return parsePrefixExpr(e)
|
return parsePrefixExpr(e)
|
||||||
case *search.InfixExpression:
|
case *search.InfixExpression:
|
||||||
return parseInfixExpr(e)
|
return parseInfixExpr(e)
|
||||||
|
case *search.StringLiteral:
|
||||||
|
return parseStringLiteral(e)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("expression type (%v) not supported", expr)
|
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")
|
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,
|
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 {
|
for _, tt := range tests {
|
||||||
|
Reference in New Issue
Block a user