gorm/model.go

288 lines
6.7 KiB
Go
Raw Normal View History

2013-10-26 12:29:39 +04:00
package gorm
import (
2013-11-04 03:58:40 +04:00
"go/ast"
"reflect"
2013-10-26 13:28:52 +04:00
"regexp"
"strconv"
2013-10-27 10:34:34 +04:00
"time"
)
2013-10-26 12:29:39 +04:00
2013-11-19 04:50:07 +04:00
var modelFieldMap = map[string][]reflect.StructField{}
2013-11-17 04:55:37 +04:00
2013-10-26 12:29:39 +04:00
type Model struct {
2013-11-02 11:17:11 +04:00
data interface{}
2013-11-11 09:40:35 +04:00
do *Do
2013-11-14 17:26:02 +04:00
_cache_fields map[string][]*Field
2013-10-26 13:28:52 +04:00
}
2013-11-15 07:36:27 +04:00
func (m *Model) reflectData() reflect.Value {
return reflect.Indirect(reflect.ValueOf(m.data))
}
2013-10-28 11:55:41 +04:00
func (m *Model) primaryKeyZero() bool {
2013-11-15 07:36:27 +04:00
return isBlank(reflect.ValueOf(m.primaryKeyValue()))
2013-10-26 19:30:17 +04:00
}
2013-11-15 07:36:27 +04:00
func (m *Model) primaryKeyValue() interface{} {
if data := m.reflectData(); data.Kind() == reflect.Struct {
2013-11-16 16:16:23 +04:00
if field := data.FieldByName(m.primaryKey()); field.IsValid() {
2013-11-15 07:36:27 +04:00
return field.Interface()
2013-10-27 05:32:49 +04:00
}
2013-10-26 19:30:17 +04:00
}
2013-11-15 07:36:27 +04:00
return 0
2013-10-26 16:53:21 +04:00
}
2013-10-28 11:55:41 +04:00
func (m *Model) primaryKey() string {
2013-10-26 13:56:00 +04:00
return "Id"
}
2013-10-28 11:55:41 +04:00
func (m *Model) primaryKeyDb() string {
return toSnake(m.primaryKey())
2013-10-26 16:20:49 +04:00
}
2013-11-17 04:55:37 +04:00
func getStructs(typ reflect.Type) (fs []reflect.StructField) {
name := typ.Name()
if fs = modelFieldMap[name]; fs != nil {
2013-11-02 13:29:56 +04:00
return
}
2013-10-26 13:28:52 +04:00
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
2013-11-04 03:58:40 +04:00
if !p.Anonymous && ast.IsExported(p.Name) {
2013-11-17 04:55:37 +04:00
fs = append(fs, p)
}
}
modelFieldMap[name] = fs
return
}
func (m *Model) fields(operation string) (fields []*Field) {
2013-11-17 05:50:34 +04:00
if len(m._cache_fields[operation]) > 0 {
return m._cache_fields[operation]
}
2013-11-17 04:55:37 +04:00
indirect_value := m.reflectData()
if !indirect_value.IsValid() {
return
}
2013-11-17 05:50:34 +04:00
structs := getStructs(indirect_value.Type())
c := make(chan *Field, len(structs))
2013-11-18 10:35:44 +04:00
defer close(c)
2013-11-17 05:50:34 +04:00
for _, field_struct := range structs {
go func(field_struct reflect.StructField, c chan *Field) {
var field Field
field.Name = field_struct.Name
field.dbName = toSnake(field_struct.Name)
field.isPrimaryKey = m.primaryKeyDb() == field.dbName
value := indirect_value.FieldByName(field_struct.Name)
field.model = m
if time_value, is_time := value.Interface().(time.Time); is_time {
field.autoCreateTime = "created_at" == field.dbName
field.autoUpdateTime = "updated_at" == field.dbName
switch operation {
case "create":
if (field.autoCreateTime || field.autoUpdateTime) && time_value.IsZero() {
value.Set(reflect.ValueOf(time.Now()))
}
case "update":
if field.autoUpdateTime {
value.Set(reflect.ValueOf(time.Now()))
}
2013-10-27 10:34:34 +04:00
}
2013-10-26 13:56:00 +04:00
}
2013-11-17 05:50:34 +04:00
field.structField = field_struct
field.reflectValue = value
field.Value = value.Interface()
field.parseAssociation()
field.parseBlank()
2014-01-23 04:50:39 +04:00
field.parseIgnore()
2013-11-17 05:50:34 +04:00
c <- &field
}(field_struct, c)
}
for i := 0; i < len(structs); i++ {
fields = append(fields, <-c)
}
2013-11-17 04:55:37 +04:00
2013-11-17 05:50:34 +04:00
if len(m._cache_fields) == 0 {
m._cache_fields = map[string][]*Field{}
2013-10-26 13:28:52 +04:00
}
2013-11-17 05:50:34 +04:00
m._cache_fields[operation] = fields
2013-11-02 11:17:11 +04:00
return
2013-10-29 13:37:45 +04:00
}
2013-11-14 17:26:02 +04:00
func (m *Model) columnsHasValue(operation string) (fields []*Field) {
2013-10-29 13:37:45 +04:00
for _, field := range m.fields(operation) {
2013-11-17 05:50:34 +04:00
if !field.isBlank {
2013-10-29 13:37:45 +04:00
fields = append(fields, field)
}
}
return
2013-10-26 12:29:39 +04:00
}
2013-11-16 09:19:29 +04:00
func (m *Model) updatedColumnsAndValues(values map[string]interface{}, ignore_protected_attrs bool) (results map[string]interface{}, any_updated bool) {
2013-11-15 07:36:27 +04:00
data := m.reflectData()
2013-11-17 17:39:50 +04:00
if !data.CanAddr() {
return values, true
2013-11-17 17:39:50 +04:00
}
for key, value := range values {
2013-11-15 07:36:27 +04:00
if field := data.FieldByName(snakeToUpperCamel(key)); field.IsValid() {
if field.Interface() != value {
switch field.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
if s, ok := value.(string); ok {
i, err := strconv.Atoi(s)
if m.do.err(err) == nil {
value = i
}
}
if field.Int() != reflect.ValueOf(value).Int() {
2013-11-12 11:21:21 +04:00
any_updated = true
2013-11-15 07:36:27 +04:00
field.SetInt(reflect.ValueOf(value).Int())
}
default:
2013-11-12 11:21:21 +04:00
any_updated = true
field.Set(reflect.ValueOf(value))
}
}
}
}
2013-11-12 11:21:21 +04:00
if values["updated_at"] != nil && any_updated {
setFieldValue(data.FieldByName("UpdatedAt"), time.Now())
}
2013-11-12 11:21:21 +04:00
return
}
2013-10-28 17:52:22 +04:00
func (m *Model) columnsAndValues(operation string) map[string]interface{} {
results := map[string]interface{}{}
2013-11-12 11:21:21 +04:00
2013-11-15 07:36:27 +04:00
for _, field := range m.fields(operation) {
2013-11-16 16:16:23 +04:00
if !field.isPrimaryKey && len(field.sqlTag()) > 0 {
2013-11-15 07:36:27 +04:00
results[field.dbName] = field.Value
2013-10-26 12:29:39 +04:00
}
}
2013-10-28 17:52:22 +04:00
return results
2013-10-26 12:29:39 +04:00
}
func (m *Model) hasColumn(name string) bool {
2013-11-16 16:16:23 +04:00
if data := m.reflectData(); data.Kind() == reflect.Struct {
2013-10-29 06:19:20 +04:00
return data.FieldByName(name).IsValid()
2013-11-15 07:36:27 +04:00
} else if data.Kind() == reflect.Slice {
return reflect.New(data.Type().Elem()).Elem().FieldByName(name).IsValid()
2013-10-29 06:19:20 +04:00
}
2013-11-15 07:36:27 +04:00
return false
}
2013-11-15 09:30:31 +04:00
func (m *Model) columnAndValue(name string) (has_column bool, is_slice bool, value interface{}) {
2013-11-16 16:16:23 +04:00
if data := m.reflectData(); data.Kind() == reflect.Struct {
2013-11-15 07:36:27 +04:00
if has_column = data.FieldByName(name).IsValid(); has_column {
value = data.FieldByName(name).Interface()
}
2013-11-15 07:36:27 +04:00
} else if data.Kind() == reflect.Slice {
has_column = reflect.New(data.Type().Elem()).Elem().FieldByName(name).IsValid()
is_slice = true
}
return
}
2013-11-16 16:16:23 +04:00
func (m *Model) typ() reflect.Type {
2013-11-15 07:36:27 +04:00
typ := m.reflectData().Type()
2013-10-29 18:00:06 +04:00
if typ.Kind() == reflect.Slice {
2013-11-16 16:16:23 +04:00
return typ.Elem()
2013-11-15 07:36:27 +04:00
} else {
2013-11-16 16:16:23 +04:00
return typ
}
}
2013-11-16 16:16:23 +04:00
func (m *Model) typeName() string {
return m.typ().Name()
}
2013-11-11 09:53:04 +04:00
func (m *Model) tableName() (str string) {
if m.data == nil {
return
}
2013-11-15 07:36:27 +04:00
data := m.reflectData()
2013-12-31 05:45:01 +04:00
if data.Kind() == reflect.Slice {
data = reflect.New(data.Type().Elem()).Elem()
}
2013-11-15 07:36:27 +04:00
if fm := data.MethodByName("TableName"); fm.IsValid() {
2013-11-12 11:21:21 +04:00
if v := fm.Call([]reflect.Value{}); len(v) > 0 {
if result, ok := v[0].Interface().(string); ok {
2013-11-11 09:53:04 +04:00
return result
}
}
}
str = toSnake(m.typeName())
2013-11-16 11:01:31 +04:00
if !m.do.db.parent.singularTable {
2013-11-06 17:43:41 +04:00
pluralMap := map[string]string{"ch": "ches", "ss": "sses", "sh": "shes", "day": "days", "y": "ies", "x": "xes", "s?": "s"}
for key, value := range pluralMap {
reg := regexp.MustCompile(key + "$")
if reg.MatchString(str) {
2013-11-11 09:53:04 +04:00
return reg.ReplaceAllString(str, value)
2013-11-06 17:43:41 +04:00
}
2013-10-28 11:49:05 +04:00
}
}
2013-10-28 08:12:12 +04:00
return
}
2013-11-11 09:53:04 +04:00
func (m *Model) callMethod(method string) {
2013-11-16 08:22:18 +04:00
if m.data == nil || m.do.db.hasError() {
2013-11-11 09:53:04 +04:00
return
2013-10-28 17:52:22 +04:00
}
2013-11-15 07:36:27 +04:00
if fm := reflect.ValueOf(m.data).MethodByName(method); fm.IsValid() {
2013-11-24 04:29:56 +04:00
numin := fm.Type().NumIn()
var results []reflect.Value
if numin == 0 {
results = fm.Call([]reflect.Value{})
} else if numin == 1 {
results = fm.Call([]reflect.Value{reflect.ValueOf(m.do.db.new())})
}
if len(results) > 0 {
if verr, ok := results[0].Interface().(error); ok {
2013-11-11 09:53:04 +04:00
m.do.err(verr)
2013-10-27 11:24:01 +04:00
}
2013-10-27 10:51:23 +04:00
}
}
2013-11-11 09:53:04 +04:00
return
2013-10-27 10:51:23 +04:00
}
func (m *Model) setValueByColumn(name string, value interface{}, out interface{}) {
2013-10-29 18:00:06 +04:00
data := reflect.Indirect(reflect.ValueOf(out))
setFieldValue(data.FieldByName(snakeToUpperCamel(name)), value)
}
2013-10-29 18:00:06 +04:00
2013-11-14 17:26:02 +04:00
func (m *Model) beforeAssociations() (fields []*Field) {
2013-11-02 13:29:56 +04:00
for _, field := range m.fields("null") {
2014-01-23 04:50:39 +04:00
if field.beforeAssociation && !field.isBlank && !field.ignoreField {
2013-11-02 13:29:56 +04:00
fields = append(fields, field)
}
}
return
}
2013-11-14 17:26:02 +04:00
func (m *Model) afterAssociations() (fields []*Field) {
2013-11-02 13:29:56 +04:00
for _, field := range m.fields("null") {
2014-01-23 04:50:39 +04:00
if field.afterAssociation && !field.isBlank && !field.ignoreField {
2013-11-02 13:29:56 +04:00
fields = append(fields, field)
}
}
return
}