2024-08-07 19:16:40 +00:00
|
|
|
package filters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2024-08-20 13:53:12 +00:00
|
|
|
"github.com/nesterow/dal/pkg/utils"
|
2024-08-07 19:16:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type In struct {
|
|
|
|
In []interface{} `json:"$in"`
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:26:29 +00:00
|
|
|
func (f In) FromJSON(data interface{}) IFilter {
|
2024-08-07 19:16:40 +00:00
|
|
|
return FromJson[In](data)
|
|
|
|
}
|
|
|
|
|
2024-08-12 18:21:34 +00:00
|
|
|
func (f In) ToSQLPart(ctx Dialect) (string, Values) {
|
2024-08-07 19:16:40 +00:00
|
|
|
if f.In == nil {
|
2024-08-12 18:21:34 +00:00
|
|
|
return "", nil
|
2024-08-07 19:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name := ctx.GetFieldName()
|
|
|
|
values := utils.Map(f.In, ctx.NormalizeValue)
|
2024-08-12 18:21:34 +00:00
|
|
|
returnValues := make(Values, 0)
|
2024-08-07 19:16:40 +00:00
|
|
|
data := make([]string, len(values))
|
2024-08-12 18:21:34 +00:00
|
|
|
for i, value := range values {
|
|
|
|
val := ValueOrPlaceholder(value).(string)
|
|
|
|
data[i] = val
|
|
|
|
if val == "?" {
|
|
|
|
returnValues = append(returnValues, value)
|
|
|
|
}
|
2024-08-07 19:16:40 +00:00
|
|
|
}
|
|
|
|
value := strings.Join(data, ", ")
|
2024-08-12 18:21:34 +00:00
|
|
|
return fmt.Sprintf("%s IN (%v)", name, value), returnValues
|
2024-08-07 19:16:40 +00:00
|
|
|
}
|