Don't use invalid value to build conditions, close #3912

This commit is contained in:
Jinzhu 2021-01-05 18:01:51 +08:00
parent 60b769c2c8
commit 00a785cd68
1 changed files with 45 additions and 44 deletions

View File

@ -308,38 +308,24 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
} }
} }
default: default:
reflectValue := reflect.Indirect(reflect.ValueOf(arg)) if reflectValue := reflect.Indirect(reflect.ValueOf(arg)); reflectValue.IsValid() {
if s, err := schema.Parse(arg, stmt.DB.cacheStore, stmt.DB.NamingStrategy); err == nil { if s, err := schema.Parse(arg, stmt.DB.cacheStore, stmt.DB.NamingStrategy); err == nil {
selectedColumns := map[string]bool{} selectedColumns := map[string]bool{}
if idx == 0 { if idx == 0 {
for _, v := range args[1:] { for _, v := range args[1:] {
if vs, ok := v.(string); ok { if vs, ok := v.(string); ok {
selectedColumns[vs] = true selectedColumns[vs] = true
}
}
}
restricted := len(selectedColumns) != 0
switch reflectValue.Kind() {
case reflect.Struct:
for _, field := range s.Fields {
selected := selectedColumns[field.DBName] || selectedColumns[field.Name]
if selected || (!restricted && field.Readable) {
if v, isZero := field.ValueOf(reflectValue); !isZero || selected {
if field.DBName != "" {
conds = append(conds, clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: field.DBName}, Value: v})
} else if field.DataType != "" {
conds = append(conds, clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: field.Name}, Value: v})
}
} }
} }
} }
case reflect.Slice, reflect.Array: restricted := len(selectedColumns) != 0
for i := 0; i < reflectValue.Len(); i++ {
switch reflectValue.Kind() {
case reflect.Struct:
for _, field := range s.Fields { for _, field := range s.Fields {
selected := selectedColumns[field.DBName] || selectedColumns[field.Name] selected := selectedColumns[field.DBName] || selectedColumns[field.Name]
if selected || (!restricted && field.Readable) { if selected || (!restricted && field.Readable) {
if v, isZero := field.ValueOf(reflectValue.Index(i)); !isZero || selected { if v, isZero := field.ValueOf(reflectValue); !isZero || selected {
if field.DBName != "" { if field.DBName != "" {
conds = append(conds, clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: field.DBName}, Value: v}) conds = append(conds, clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: field.DBName}, Value: v})
} else if field.DataType != "" { } else if field.DataType != "" {
@ -348,29 +334,44 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
} }
} }
} }
}
}
if restricted {
break
}
} else if len(conds) == 0 {
if len(args) == 1 {
switch reflectValue.Kind() {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
values := make([]interface{}, reflectValue.Len())
for i := 0; i < reflectValue.Len(); i++ { for i := 0; i < reflectValue.Len(); i++ {
values[i] = reflectValue.Index(i).Interface() for _, field := range s.Fields {
selected := selectedColumns[field.DBName] || selectedColumns[field.Name]
if selected || (!restricted && field.Readable) {
if v, isZero := field.ValueOf(reflectValue.Index(i)); !isZero || selected {
if field.DBName != "" {
conds = append(conds, clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: field.DBName}, Value: v})
} else if field.DataType != "" {
conds = append(conds, clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: field.Name}, Value: v})
}
}
}
}
} }
if len(values) > 0 {
conds = append(conds, clause.IN{Column: clause.PrimaryColumn, Values: values})
}
return conds
} }
}
conds = append(conds, clause.IN{Column: clause.PrimaryColumn, Values: args}) if restricted {
break
}
} else if len(conds) == 0 {
if len(args) == 1 {
switch reflectValue.Kind() {
case reflect.Slice, reflect.Array:
values := make([]interface{}, reflectValue.Len())
for i := 0; i < reflectValue.Len(); i++ {
values[i] = reflectValue.Index(i).Interface()
}
if len(values) > 0 {
conds = append(conds, clause.IN{Column: clause.PrimaryColumn, Values: values})
}
return conds
}
}
conds = append(conds, clause.IN{Column: clause.PrimaryColumn, Values: args})
}
} }
} }
} }