netquery: fix value encoding for time.Time queries

This commit is contained in:
Patrick Pacher
2023-09-13 10:27:00 +02:00
parent 87f19bdcc2
commit c58a73081f
2 changed files with 36 additions and 8 deletions

View File

@@ -182,7 +182,17 @@ func DatetimeEncoder(loc *time.Location) EncodeFunc {
} }
case (normalizedKind == reflect.Int || normalizedKind == reflect.Uint || normalizedKind == reflect.Float64) && colDef.IsTime: case (normalizedKind == reflect.Int || normalizedKind == reflect.Uint || normalizedKind == reflect.Float64) && colDef.IsTime:
t = time.Unix(val.Int(), 0) seconds := int64(0)
switch normalizedKind {
case reflect.Int:
seconds = val.Int()
case reflect.Uint:
seconds = int64(val.Uint())
case reflect.Float64:
seconds = int64(val.Float())
}
t = time.Unix(seconds, 0)
default: default:
// we don't care ... // we don't care ...

View File

@@ -327,12 +327,30 @@ func (match Matcher) toSQLConditionClause(ctx context.Context, suffix string, co
var placeholder []string var placeholder []string
for idx, value := range values { for idx, value := range values {
encodedValue, err := orm.EncodeValue(ctx, &colDef, value, encoderConfig) var (
if err != nil { encodedValue any = value
errs.Errors = append(errs.Errors, err error
fmt.Errorf("failed to encode %v for column %s: %w", value, colDef.Name, err), )
)
return kind := orm.NormalizeKind(reflect.TypeOf(value).Kind())
isNumber := slices.Contains([]reflect.Kind{
reflect.Uint,
reflect.Int,
reflect.Float64,
}, kind)
// if we query a time-field that is queried as a number, don't do any encoding
// here as the orm.DateTimeEncoder would convert the number to a string.
if colDef.IsTime && colDef.Type == sqlite.TypeText && isNumber {
encodedValue = value
} else {
encodedValue, err = orm.EncodeValue(ctx, &colDef, value, encoderConfig)
if err != nil {
errs.Errors = append(errs.Errors,
fmt.Errorf("failed to encode %v for column %s: %w", value, colDef.Name, err),
)
return
}
} }
uniqKey := fmt.Sprintf(":%s%s%d", key, suffix, idx) uniqKey := fmt.Sprintf(":%s%s%d", key, suffix, idx)
@@ -340,7 +358,7 @@ func (match Matcher) toSQLConditionClause(ctx context.Context, suffix string, co
params[uniqKey] = encodedValue params[uniqKey] = encodedValue
} }
// NOTE(ppacher): for now we assume that the type of each member of values // NOTE(ppacher): for now we assume that the type of each element of values
// is the same. We also can be sure that there is always at least one value. // is the same. We also can be sure that there is always at least one value.
// //
// FIXME(ppacher): if we start supporting values of different types here // FIXME(ppacher): if we start supporting values of different types here