/
filter.go
41 lines (35 loc) · 1.23 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package gormimpl
import (
"fmt"
"github.com/flyteorg/flyte/datacatalog/pkg/common"
"github.com/flyteorg/flyte/datacatalog/pkg/repositories/errors"
"github.com/flyteorg/flyte/datacatalog/pkg/repositories/models"
)
// String formats for various GORM expression queries
const (
equalQuery = "%s.%s = ?"
)
type gormValueFilterImpl struct {
comparisonOperator common.ComparisonOperator
field string
value interface{}
}
// Get the GORM expression to filter by a model's property. The output should be a valid input into tx.Where()
func (g *gormValueFilterImpl) GetDBQueryExpression(tableName string) (models.DBQueryExpr, error) {
switch g.comparisonOperator {
case common.Equal:
return models.DBQueryExpr{
Query: fmt.Sprintf(equalQuery, tableName, g.field),
Args: g.value,
}, nil
}
return models.DBQueryExpr{}, errors.GetUnsupportedFilterExpressionErr(g.comparisonOperator)
}
// Construct the container necessary to issue a db query to filter in GORM
func NewGormValueFilter(comparisonOperator common.ComparisonOperator, field string, value interface{}) models.ModelValueFilter {
return &gormValueFilterImpl{
comparisonOperator: comparisonOperator,
field: field,
value: value,
}
}