作者 yangfu

fix: incorrect query when value is null

... ... @@ -141,7 +141,11 @@ func (c Condition) SetWhere(params QueryOptions, q *gorm.DB) {
}
if len(c.In) > 0 {
if c.Field.SQLType == domain.Float.ToString() {
q.Where(fmt.Sprintf("%v in %v", c.CastType(c.Field.SQLName, domain.DECIMALV2.ToString()), c.InArgs(c.In)))
if hasEmpty(c.In) {
q.Where(fmt.Sprintf("((%v in %v) or (%v is null))", c.CastType(c.Field.SQLName, domain.DECIMALV2.ToString()), c.InArgs(c.In), c.Field.SQLName))
} else {
q.Where(fmt.Sprintf("%v in %v", c.CastType(c.Field.SQLName, domain.DECIMALV2.ToString()), c.InArgs(c.In)))
}
} else {
q.Where(fmt.Sprintf("%v in %v", c.CastType(c.FormatIfNull(params, c.Field), "string"), c.InArgs(c.In)))
}
... ... @@ -222,6 +226,15 @@ func formatFiled(f *domain.Field) string {
return f.SQLName
}
func hasEmpty(in []interface{}) bool {
for _, arg := range in {
if v, ok := arg.(string); ok && len(v) == 0 {
return true
}
}
return false
}
func castType(sql, t string) string {
return fmt.Sprintf("cast(%v as %v)", sql, t)
}
... ...