gorm/field.go

130 lines
2.6 KiB
Go
Raw Normal View History

2013-11-14 17:26:02 +04:00
package gorm
import (
"database/sql"
2013-11-15 07:36:27 +04:00
"reflect"
2013-11-14 17:26:02 +04:00
"strconv"
"strings"
2013-11-15 07:36:27 +04:00
"time"
2013-11-14 17:26:02 +04:00
)
type Field struct {
2013-11-14 17:38:35 +04:00
Name string
Value interface{}
2013-11-15 07:36:27 +04:00
model *Model
dbName string
isPrimaryKey bool
autoCreateTime bool
autoUpdateTime bool
foreignKey string
2013-11-14 17:26:02 +04:00
beforeAssociation bool
afterAssociation bool
2013-11-15 07:36:27 +04:00
reflectValue reflect.Value
structField reflect.StructField
2013-11-14 17:26:02 +04:00
}
2013-11-14 17:38:35 +04:00
func (f *Field) isBlank() bool {
2013-11-15 07:36:27 +04:00
return isBlank(f.reflectValue)
}
func (f *Field) isScanner() bool {
_, is_scanner := reflect.New(f.reflectValue.Type()).Interface().(sql.Scanner)
return is_scanner
}
func (f *Field) isTime() bool {
_, is_time := f.Value.(time.Time)
return is_time
2013-11-14 17:38:35 +04:00
}
2013-11-15 07:36:27 +04:00
func (f *Field) sqlTag() (str string) {
value := f.Value
if f.isScanner() {
value = f.reflectValue.Field(0).Interface()
}
reflect_value := f.reflectValue
switch reflect_value.Kind() {
2013-11-14 17:26:02 +04:00
case reflect.Slice:
2013-11-15 07:36:27 +04:00
return
2013-11-14 17:26:02 +04:00
case reflect.Struct:
2013-11-15 07:36:27 +04:00
if !f.isTime() && !f.isScanner() {
return
2013-11-14 17:26:02 +04:00
}
}
typ, addational_typ, size := parseSqlTag(f.structField.Tag.Get(tagIdentifier))
if typ == "-" {
2013-11-15 07:36:27 +04:00
return
2013-11-14 17:26:02 +04:00
}
if len(typ) == 0 {
2013-11-15 07:36:27 +04:00
if f.isPrimaryKey {
typ = f.model.do.chain.d.dialect.PrimaryKeyTag(value, size)
2013-11-14 17:26:02 +04:00
} else {
2013-11-15 07:36:27 +04:00
typ = f.model.do.chain.d.dialect.SqlTag(value, size)
2013-11-14 17:26:02 +04:00
}
}
if len(addational_typ) > 0 {
typ = typ + " " + addational_typ
}
return typ
}
2013-11-14 17:38:35 +04:00
func (f *Field) parseAssociation() {
2013-11-15 07:36:27 +04:00
reflect_value := f.reflectValue
2013-11-14 17:38:35 +04:00
2013-11-15 07:36:27 +04:00
switch reflect_value.Kind() {
2013-11-14 17:38:35 +04:00
case reflect.Slice:
foreign_key := f.model.typeName() + "Id"
2013-11-15 07:36:27 +04:00
if reflect.New(reflect_value.Type().Elem()).Elem().FieldByName(foreign_key).IsValid() {
2013-11-14 17:38:35 +04:00
f.foreignKey = foreign_key
}
f.afterAssociation = true
case reflect.Struct:
2013-11-15 07:36:27 +04:00
if !f.isTime() && !f.isScanner() {
if f.model.reflectData().FieldByName(f.Name + "Id").IsValid() {
2013-11-14 17:38:35 +04:00
f.foreignKey = f.Name + "Id"
f.beforeAssociation = true
} else {
foreign_key := f.model.typeName() + "Id"
2013-11-15 07:36:27 +04:00
if reflect.New(reflect_value.Type()).Elem().FieldByName(foreign_key).IsValid() {
2013-11-14 17:38:35 +04:00
f.foreignKey = foreign_key
}
f.afterAssociation = true
}
}
}
}
2013-11-14 17:26:02 +04:00
func parseSqlTag(str string) (typ string, addational_typ string, size int) {
if str == "-" {
typ = str
} else if str != "" {
tags := strings.Split(str, ";")
m := make(map[string]string)
for _, value := range tags {
v := strings.Split(value, ":")
k := strings.Trim(strings.ToUpper(v[0]), " ")
if len(v) == 2 {
m[k] = v[1]
} else {
m[k] = k
}
}
if len(m["SIZE"]) > 0 {
size, _ = strconv.Atoi(m["SIZE"])
}
if len(m["TYPE"]) > 0 {
typ = m["TYPE"]
}
addational_typ = m["NOT NULL"] + " " + m["UNIQUE"]
}
return
}