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
|
2014-01-29 06:35:28 +04:00
|
|
|
Tag reflect.StructTag
|
2014-01-27 04:26:59 +04:00
|
|
|
SqlTag string
|
|
|
|
ForeignKey string
|
|
|
|
BeforeAssociation bool
|
|
|
|
AfterAssociation bool
|
2014-01-28 12:06:22 +04:00
|
|
|
isPrimaryKey bool
|
2013-11-14 17:26:02 +04:00
|
|
|
}
|
|
|
|
|
2014-01-26 15:34:06 +04:00
|
|
|
func (f *Field) IsScanner() bool {
|
2014-07-25 12:51:54 +04:00
|
|
|
_, isScanner := reflect.New(reflect.ValueOf(f.Value).Type()).Interface().(sql.Scanner)
|
|
|
|
return isScanner
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Field) IsTime() bool {
|
2014-07-25 12:51:54 +04:00
|
|
|
_, isTime := f.Value.(time.Time)
|
|
|
|
return isTime
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|
|
|
|
|
2014-07-25 11:49:10 +04:00
|
|
|
func parseSqlTag(str string) (typ string, additionalType string, size int) {
|
2013-11-14 17:26:02 +04:00
|
|
|
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"]
|
|
|
|
}
|
|
|
|
|
2014-07-25 11:49:10 +04:00
|
|
|
additionalType = m["NOT NULL"] + " " + m["UNIQUE"]
|
2014-07-22 01:33:41 +04:00
|
|
|
|
|
|
|
if len(m["DEFAULT"]) > 0 {
|
2014-07-25 11:49:10 +04:00
|
|
|
additionalType = additionalType + "DEFAULT " + m["DEFAULT"]
|
2014-07-22 01:33:41 +04:00
|
|
|
}
|
|
|
|
|
2013-11-14 17:26:02 +04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|