gorm/field.go

175 lines
3.5 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 {
2014-01-27 04:26:59 +04:00
Name string
DBName string
Value interface{}
IsBlank bool
IsIgnored bool
Tag string
AddationalTag string
Size int
SqlTag string
ForeignKey string
BeforeAssociation bool
AfterAssociation bool
2013-11-15 07:36:27 +04:00
foreignKey string
2013-11-14 17:26:02 +04:00
beforeAssociation bool
afterAssociation bool
2014-01-27 04:26:59 +04:00
dbName string
model *Model
isBlank bool
ignoreField bool
isPrimaryKey bool
autoCreateTime bool
autoUpdateTime bool
reflectValue reflect.Value
structField reflect.StructField
2013-11-14 17:26:02 +04:00
}
2014-01-26 15:34:06 +04:00
func (f *Field) IsScanner() bool {
_, is_scanner := reflect.New(reflect.ValueOf(f.Value).Type()).Interface().(sql.Scanner)
return is_scanner
}
func (f *Field) IsTime() bool {
_, is_time := f.Value.(time.Time)
return is_time
}
2014-01-27 04:26:59 +04:00
func (f *Field) parseAssociation() {
elem := reflect.Indirect(reflect.ValueOf(f.Value))
typ := elem.Type()
switch elem.Kind() {
case reflect.Slice:
typ = typ.Elem()
if _, ok := f.Value.([]byte); !ok {
foreignKey := typ.Name() + "Id"
if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
f.ForeignKey = foreignKey
f.foreignKey = foreignKey
}
f.AfterAssociation = true
f.afterAssociation = true
}
case reflect.Struct:
if !f.IsTime() && !f.IsScanner() {
if elem.FieldByName(f.Name + "Id").IsValid() {
f.foreignKey = f.Name + "Id"
f.beforeAssociation = true
f.ForeignKey = f.Name + "Id"
f.BeforeAssociation = true
} else {
foreignKey := typ.Name() + "Id"
if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
f.foreignKey = foreignKey
f.ForeignKey = foreignKey
}
f.afterAssociation = true
f.AfterAssociation = true
}
}
}
}
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 {
2014-01-26 15:34:06 +04:00
_, is_scanner := reflect.New(reflect.ValueOf(f.Value).Type()).Interface().(sql.Scanner)
2013-11-15 07:36:27 +04:00
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
}
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
}