gorm/field.go

144 lines
2.9 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
2013-11-17 05:50:34 +04:00
isBlank bool
2014-01-23 04:50:39 +04:00
ignoreField bool
2013-11-15 07:36:27 +04:00
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-17 05:50:34 +04:00
func (f *Field) parseBlank() {
f.isBlank = isBlank(f.reflectValue)
2013-11-15 07:36:27 +04:00
}
2014-01-23 04:50:39 +04:00
func (f *Field) parseIgnore() {
typ, _, _ := parseSqlTag(f.structField.Tag.Get(f.model.do.db.parent.tagIdentifier))
if typ == "-" {
f.ignoreField = true
}
}
2013-11-15 07:36:27 +04:00
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-21 09:47:49 +04:00
if _, ok := f.Value.([]byte); !ok {
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
}
}
2013-11-16 14:01:44 +04:00
typ, addational_typ, size := parseSqlTag(f.structField.Tag.Get(f.model.do.db.parent.tagIdentifier))
2013-11-14 17:26:02 +04:00
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 {
2013-11-16 09:28:22 +04:00
typ = f.model.do.dialect().PrimaryKeyTag(value, size)
2013-11-14 17:26:02 +04:00
} else {
2013-11-16 09:28:22 +04:00
typ = f.model.do.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:
2013-11-21 09:47:49 +04:00
if _, ok := f.Value.([]byte); !ok {
foreign_key := f.model.typeName() + "Id"
if reflect.New(reflect_value.Type().Elem()).Elem().FieldByName(foreign_key).IsValid() {
f.foreignKey = foreign_key
}
f.afterAssociation = true
2013-11-14 17:38:35 +04:00
}
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, ":")
2013-11-17 04:28:30 +04:00
k := strings.TrimSpace(strings.ToUpper(v[0]))
2013-11-14 17:26:02 +04:00
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
}