2013-11-14 17:26:02 +04:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2014-09-30 16:02:51 +04:00
|
|
|
"errors"
|
2013-11-15 07:36:27 +04:00
|
|
|
"reflect"
|
|
|
|
"time"
|
2013-11-14 17:26:02 +04:00
|
|
|
)
|
|
|
|
|
2014-07-30 16:59:52 +04:00
|
|
|
type relationship struct {
|
2014-07-31 07:08:26 +04:00
|
|
|
JoinTable string
|
|
|
|
ForeignKey string
|
|
|
|
AssociationForeignKey string
|
|
|
|
Kind string
|
2014-07-30 07:32:18 +04:00
|
|
|
}
|
|
|
|
|
2013-11-14 17:26:02 +04:00
|
|
|
type Field struct {
|
2014-07-30 17:14:10 +04:00
|
|
|
Name string
|
|
|
|
DBName string
|
2014-08-28 14:25:05 +04:00
|
|
|
Field reflect.Value
|
2014-07-30 17:14:10 +04:00
|
|
|
Tag reflect.StructTag
|
|
|
|
Relationship *relationship
|
2014-08-30 18:39:28 +04:00
|
|
|
IsNormal bool
|
2014-07-31 07:08:26 +04:00
|
|
|
IsBlank bool
|
|
|
|
IsIgnored bool
|
|
|
|
IsPrimaryKey bool
|
2013-11-14 17:26:02 +04:00
|
|
|
}
|
|
|
|
|
2014-09-02 15:03:01 +04:00
|
|
|
func (field *Field) IsScanner() bool {
|
|
|
|
_, isScanner := reflect.New(field.Field.Type()).Interface().(sql.Scanner)
|
2014-07-25 12:51:54 +04:00
|
|
|
return isScanner
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|
|
|
|
|
2014-09-02 15:03:01 +04:00
|
|
|
func (field *Field) IsTime() bool {
|
|
|
|
_, isTime := field.Field.Interface().(time.Time)
|
2014-07-25 12:51:54 +04:00
|
|
|
return isTime
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|
2014-09-02 15:03:01 +04:00
|
|
|
|
2014-09-30 16:02:51 +04:00
|
|
|
func (field *Field) Set(value interface{}) (err error) {
|
|
|
|
if !field.Field.IsValid() {
|
|
|
|
return errors.New("field value not valid")
|
2014-09-02 15:03:01 +04:00
|
|
|
}
|
2014-09-30 16:02:51 +04:00
|
|
|
|
|
|
|
if !field.Field.CanAddr() {
|
|
|
|
return errors.New("field value not addressable")
|
|
|
|
}
|
|
|
|
|
|
|
|
if rvalue, ok := value.(reflect.Value); ok {
|
|
|
|
value = rvalue.Interface()
|
2014-09-02 15:03:01 +04:00
|
|
|
}
|
2014-09-30 16:02:51 +04:00
|
|
|
|
|
|
|
if scanner, ok := field.Field.Addr().Interface().(sql.Scanner); ok {
|
|
|
|
scanner.Scan(value)
|
|
|
|
} else if reflect.TypeOf(value).ConvertibleTo(field.Field.Type()) {
|
|
|
|
field.Field.Set(reflect.ValueOf(value).Convert(field.Field.Type()))
|
|
|
|
} else {
|
|
|
|
return errors.New("could not convert argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
field.IsBlank = isBlank(field.Field)
|
|
|
|
|
2014-09-02 15:03:01 +04:00
|
|
|
return
|
|
|
|
}
|