mirror of https://github.com/go-gorm/gorm.git
Only query with readable fields
This commit is contained in:
parent
cb5a35a807
commit
9bfe306975
24
statement.go
24
statement.go
|
@ -271,22 +271,26 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) (c
|
||||||
switch reflectValue.Kind() {
|
switch reflectValue.Kind() {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
for _, field := range s.Fields {
|
for _, field := range s.Fields {
|
||||||
if v, isZero := field.ValueOf(reflectValue); !isZero {
|
if field.Readable {
|
||||||
if field.DBName == "" {
|
if v, isZero := field.ValueOf(reflectValue); !isZero {
|
||||||
conds = append(conds, clause.Eq{Column: clause.Column{Table: s.Table, Name: field.Name}, Value: v})
|
if field.DBName == "" {
|
||||||
} else {
|
conds = append(conds, clause.Eq{Column: clause.Column{Table: s.Table, Name: field.Name}, Value: v})
|
||||||
conds = append(conds, clause.Eq{Column: clause.Column{Table: s.Table, Name: field.DBName}, Value: v})
|
} else {
|
||||||
|
conds = append(conds, clause.Eq{Column: clause.Column{Table: s.Table, Name: field.DBName}, Value: v})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Slice, reflect.Array:
|
case reflect.Slice, reflect.Array:
|
||||||
for i := 0; i < reflectValue.Len(); i++ {
|
for i := 0; i < reflectValue.Len(); i++ {
|
||||||
for _, field := range s.Fields {
|
for _, field := range s.Fields {
|
||||||
if v, isZero := field.ValueOf(reflectValue.Index(i)); !isZero {
|
if field.Readable {
|
||||||
if field.DBName == "" {
|
if v, isZero := field.ValueOf(reflectValue.Index(i)); !isZero {
|
||||||
conds = append(conds, clause.Eq{Column: clause.Column{Table: s.Table, Name: field.Name}, Value: v})
|
if field.DBName == "" {
|
||||||
} else {
|
conds = append(conds, clause.Eq{Column: clause.Column{Table: s.Table, Name: field.Name}, Value: v})
|
||||||
conds = append(conds, clause.Eq{Column: clause.Column{Table: s.Table, Name: field.DBName}, Value: v})
|
} else {
|
||||||
|
conds = append(conds, clause.Eq{Column: clause.Column{Table: s.Table, Name: field.DBName}, Value: v})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,10 +134,18 @@ func TestCustomizeField(t *testing.T) {
|
||||||
t.Fatalf("invalid updated result: %#v", result2)
|
t.Fatalf("invalid updated result: %#v", result2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := DB.Where(CustomizeFieldStruct{Name: create.Name, FieldReadonly: create.FieldReadonly, FieldIgnore: create.FieldIgnore}).First(&CustomizeFieldStruct{}).Error; err == nil {
|
||||||
|
t.Fatalf("Should failed to find result")
|
||||||
|
}
|
||||||
|
|
||||||
if err := DB.Table("customize_field_structs").Where("1 = 1").UpdateColumn("field_readonly", "readonly").Error; err != nil {
|
if err := DB.Table("customize_field_structs").Where("1 = 1").UpdateColumn("field_readonly", "readonly").Error; err != nil {
|
||||||
t.Fatalf("failed to update field_readonly column")
|
t.Fatalf("failed to update field_readonly column")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := DB.Where(CustomizeFieldStruct{Name: create.Name, FieldReadonly: "readonly", FieldIgnore: create.FieldIgnore}).First(&CustomizeFieldStruct{}).Error; err != nil {
|
||||||
|
t.Fatalf("Should find result")
|
||||||
|
}
|
||||||
|
|
||||||
var result3 CustomizeFieldStruct
|
var result3 CustomizeFieldStruct
|
||||||
DB.Find(&result3, "name = ?", "create")
|
DB.Find(&result3, "name = ?", "create")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue