作者 yangfu

fix: incorrect query when value is null

@@ -141,7 +141,11 @@ func (c Condition) SetWhere(params QueryOptions, q *gorm.DB) { @@ -141,7 +141,11 @@ func (c Condition) SetWhere(params QueryOptions, q *gorm.DB) {
141 } 141 }
142 if len(c.In) > 0 { 142 if len(c.In) > 0 {
143 if c.Field.SQLType == domain.Float.ToString() { 143 if c.Field.SQLType == domain.Float.ToString() {
144 - q.Where(fmt.Sprintf("%v in %v", c.CastType(c.Field.SQLName, domain.DECIMALV2.ToString()), c.InArgs(c.In))) 144 + if hasEmpty(c.In) {
  145 + 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))
  146 + } else {
  147 + q.Where(fmt.Sprintf("%v in %v", c.CastType(c.Field.SQLName, domain.DECIMALV2.ToString()), c.InArgs(c.In)))
  148 + }
145 } else { 149 } else {
146 q.Where(fmt.Sprintf("%v in %v", c.CastType(c.FormatIfNull(params, c.Field), "string"), c.InArgs(c.In))) 150 q.Where(fmt.Sprintf("%v in %v", c.CastType(c.FormatIfNull(params, c.Field), "string"), c.InArgs(c.In)))
147 } 151 }
@@ -222,6 +226,15 @@ func formatFiled(f *domain.Field) string { @@ -222,6 +226,15 @@ func formatFiled(f *domain.Field) string {
222 return f.SQLName 226 return f.SQLName
223 } 227 }
224 228
  229 +func hasEmpty(in []interface{}) bool {
  230 + for _, arg := range in {
  231 + if v, ok := arg.(string); ok && len(v) == 0 {
  232 + return true
  233 + }
  234 + }
  235 + return false
  236 +}
  237 +
225 func castType(sql, t string) string { 238 func castType(sql, t string) string {
226 return fmt.Sprintf("cast(%v as %v)", sql, t) 239 return fmt.Sprintf("cast(%v as %v)", sql, t)
227 } 240 }