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 {
|
|
|
|
_, 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
|
|
|
|
}
|
|
|
|
|
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"]
|
2014-07-22 01:33:41 +04:00
|
|
|
|
|
|
|
if len(m["DEFAULT"]) > 0 {
|
|
|
|
addational_typ = addational_typ + "DEFAULT " + m["DEFAULT"]
|
|
|
|
}
|
|
|
|
|
2013-11-14 17:26:02 +04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|