Refactor Fields

This commit is contained in:
Jinzhu 2015-02-16 17:47:07 +08:00
parent 2d866090e4
commit de3f2a5c46
3 changed files with 18 additions and 50 deletions

View File

@ -38,19 +38,20 @@ func (field *Field) Set(value interface{}) (err error) {
return return
} }
type relationship struct { // Fields get value's fields
JoinTable string func (scope *Scope) Fields() map[string]*Field {
ForeignKey string fields := map[string]*Field{}
ForeignType string structFields := scope.GetStructFields()
AssociationForeignKey string
Kind string for _, structField := range structFields {
fields[structField.DBName] = scope.getField(structField)
}
return fields
} }
// FIXME func (scope *Scope) getField(structField *StructField) *Field {
func (r relationship) ForeignDBName() string { field := Field{StructField: structField}
return ToSnake(r.ForeignKey) field.Field = scope.IndirectValue().FieldByName(structField.Name)
} return &field
func (r relationship) AssociationForeignDBName(name string) string {
return ToSnake(r.AssociationForeignKey)
} }

View File

@ -43,7 +43,7 @@ type Relationship struct {
JoinTable string JoinTable string
} }
func (scope *Scope) GenerateSqlTag(field *StructField) { func (scope *Scope) generateSqlTag(field *StructField) {
var sqlType string var sqlType string
reflectValue := reflect.Indirect(reflect.New(field.Struct.Type)) reflectValue := reflect.Indirect(reflect.New(field.Struct.Type))
@ -246,7 +246,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
modelStruct.PrimaryKeyField = field modelStruct.PrimaryKeyField = field
} }
scope.GenerateSqlTag(field) scope.generateSqlTag(field)
} }
} }

View File

@ -95,28 +95,7 @@ func (scope *Scope) HasError() bool {
} }
func (scope *Scope) PrimaryKeyField() *Field { func (scope *Scope) PrimaryKeyField() *Field {
if scope.primaryKeyField == nil { return scope.getField(scope.GetModelStruct().PrimaryKeyField)
var indirectValue = scope.IndirectValue()
clone := scope
// FIXME
if indirectValue.Kind() == reflect.Slice {
typ := indirectValue.Type().Elem()
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
clone = scope.New(reflect.New(typ).Elem().Addr().Interface())
}
for _, field := range clone.Fields() {
if field.IsPrimaryKey {
scope.primaryKeyField = field
break
}
}
}
return scope.primaryKeyField
} }
// PrimaryKey get the primary key's column name // PrimaryKey get the primary key's column name
@ -135,7 +114,7 @@ func (scope *Scope) PrimaryKeyZero() bool {
// PrimaryKeyValue get the primary key's value // PrimaryKeyValue get the primary key's value
func (scope *Scope) PrimaryKeyValue() interface{} { func (scope *Scope) PrimaryKeyValue() interface{} {
if field := scope.PrimaryKeyField(); field != nil { if field := scope.PrimaryKeyField(); field != nil && field.Field.IsValid() {
return field.Field.Interface() return field.Field.Interface()
} else { } else {
return 0 return 0
@ -294,18 +273,6 @@ func (scope *Scope) FieldByName(name string) (field *Field, ok bool) {
return nil, false return nil, false
} }
// Fields get value's fields
func (scope *Scope) Fields() map[string]*Field {
fields := map[string]*Field{}
structFields := scope.GetStructFields()
for _, structField := range structFields {
field := Field{StructField: structField}
fields[field.DBName] = &field
}
return fields
}
// Raw set sql // Raw set sql
func (scope *Scope) Raw(sql string) *Scope { func (scope *Scope) Raw(sql string) *Scope {
scope.Sql = strings.Replace(sql, "$$", "?", -1) scope.Sql = strings.Replace(sql, "$$", "?", -1)