This commit is contained in:
Jinzhu 2014-01-29 10:35:28 +08:00
parent b713479174
commit 241b6bc3b9
2 changed files with 13 additions and 18 deletions

View File

@ -14,9 +14,7 @@ type Field struct {
Value interface{} Value interface{}
IsBlank bool IsBlank bool
IsIgnored bool IsIgnored bool
Tag string Tag reflect.StructTag
AddationalTag string
Size int
SqlTag string SqlTag string
ForeignKey string ForeignKey string
BeforeAssociation bool BeforeAssociation bool

View File

@ -242,6 +242,12 @@ func (scope *Scope) CombinedConditionSql() string {
} }
func (scope *Scope) SqlTagForField(field *Field) (tag string) { func (scope *Scope) SqlTagForField(field *Field) (tag string) {
tag, addationalTag, size := parseSqlTag(field.Tag.Get(scope.db.parent.tagIdentifier))
if tag == "-" {
field.IsIgnored = true
}
value := field.Value value := field.Value
reflectValue := reflect.ValueOf(value) reflectValue := reflect.ValueOf(value)
@ -260,17 +266,16 @@ func (scope *Scope) SqlTagForField(field *Field) (tag string) {
} }
} }
tag = field.Tag
if len(tag) == 0 && tag != "-" { if len(tag) == 0 && tag != "-" {
if field.isPrimaryKey { if field.isPrimaryKey {
tag = scope.Dialect().PrimaryKeyTag(value, field.Size) tag = scope.Dialect().PrimaryKeyTag(value, size)
} else { } else {
tag = scope.Dialect().SqlTag(value, field.Size) tag = scope.Dialect().SqlTag(value, size)
} }
} }
if len(field.AddationalTag) > 0 { if len(addationalTag) > 0 {
tag = tag + " " + field.AddationalTag tag = tag + " " + addationalTag
} }
return return
} }
@ -297,20 +302,12 @@ func (scope *Scope) Fields() []*Field {
value := indirectValue.FieldByName(fieldStruct.Name) value := indirectValue.FieldByName(fieldStruct.Name)
field.Value = value.Interface() field.Value = value.Interface()
field.IsBlank = isBlank(value) field.IsBlank = isBlank(value)
field.isPrimaryKey = scope.PrimaryKey() == field.DBName
if scope.db != nil { if scope.db != nil {
tag, addationalTag, size := parseSqlTag(fieldStruct.Tag.Get(scope.db.parent.tagIdentifier)) field.Tag = fieldStruct.Tag
field.Tag = tag
field.AddationalTag = addationalTag
field.isPrimaryKey = scope.PrimaryKey() == field.DBName
field.Size = size
field.SqlTag = scope.SqlTagForField(&field) field.SqlTag = scope.SqlTagForField(&field)
if tag == "-" {
field.IsIgnored = true
}
// parse association // parse association
elem := reflect.Indirect(value) elem := reflect.Indirect(value)
typ := elem.Type() typ := elem.Type()