gorm/model.go

379 lines
8.8 KiB
Go
Raw Normal View History

2013-10-26 12:29:39 +04:00
package gorm
import (
2013-11-10 04:14:41 +04:00
"database/sql"
2013-10-28 08:12:12 +04:00
"errors"
2013-10-26 13:28:52 +04:00
"fmt"
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
type Model struct {
2013-11-02 11:17:11 +04:00
data interface{}
driver string
2013-11-10 14:33:37 +04:00
debug bool
2013-11-02 11:17:11 +04:00
_cache_fields map[string][]Field
2013-10-26 13:28:52 +04:00
}
type Field struct {
2013-10-27 10:34:34 +04:00
Name string
Value interface{}
SqlType string
DbName string
AutoCreateTime bool
AutoUpdateTime bool
IsPrimaryKey bool
2013-10-29 13:37:45 +04:00
IsBlank bool
2013-11-02 13:29:56 +04:00
beforeAssociation bool
afterAssociation bool
2013-11-02 16:05:05 +04:00
foreignKey string
2013-10-26 13:28:52 +04:00
}
2013-10-28 11:55:41 +04:00
func (m *Model) primaryKeyZero() bool {
2013-10-29 03:39:26 +04:00
return m.primaryKeyValue() <= 0
2013-10-26 19:30:17 +04:00
}
2013-10-28 11:55:41 +04:00
func (m *Model) primaryKeyValue() int64 {
2013-10-29 03:39:26 +04:00
if m.data == nil {
return -1
2013-10-28 16:27:25 +04:00
}
2013-11-01 07:43:41 +04:00
data := reflect.Indirect(reflect.ValueOf(m.data))
2013-10-29 18:00:06 +04:00
switch data.Kind() {
2013-10-26 19:30:17 +04:00
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
return 0
default:
2013-10-29 18:00:06 +04:00
value := data.FieldByName(m.primaryKey())
2013-10-27 05:32:49 +04:00
if value.IsValid() {
2013-10-29 18:00:06 +04:00
switch value.Kind() {
case reflect.Int, reflect.Int64, reflect.Int32:
return value.Int()
default:
return 0
}
2013-10-27 05:32:49 +04:00
} else {
return 0
}
2013-10-26 19:30:17 +04:00
}
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-10-28 11:55:41 +04:00
func (m *Model) fields(operation string) (fields []Field) {
2013-11-02 11:17:11 +04:00
if len(m._cache_fields[operation]) > 0 {
2013-11-02 13:29:56 +04:00
return m._cache_fields[operation]
2013-11-02 11:17:11 +04:00
}
2013-10-29 13:37:45 +04:00
indirect_value := reflect.Indirect(reflect.ValueOf(m.data))
2013-11-02 13:29:56 +04:00
if !indirect_value.IsValid() {
return
}
2013-10-29 13:37:45 +04:00
typ := indirect_value.Type()
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-10-26 13:28:52 +04:00
var field Field
field.Name = p.Name
field.DbName = toSnake(p.Name)
2013-10-28 11:55:41 +04:00
field.IsPrimaryKey = m.primaryKeyDb() == field.DbName
2013-10-27 10:34:34 +04:00
field.AutoCreateTime = "created_at" == field.DbName
field.AutoUpdateTime = "updated_at" == field.DbName
2013-10-29 13:37:45 +04:00
value := indirect_value.FieldByName(p.Name)
2013-11-02 10:12:18 +04:00
time_value, is_time := value.Interface().(time.Time)
2013-10-29 13:37:45 +04:00
switch value.Kind() {
case reflect.Int, reflect.Int64, reflect.Int32:
field.IsBlank = value.Int() == 0
case reflect.String:
field.IsBlank = value.String() == ""
2013-11-02 13:29:56 +04:00
case reflect.Slice:
if value.Len() == 0 {
field.IsBlank = true
}
case reflect.Struct:
2013-11-02 10:12:18 +04:00
if is_time {
field.IsBlank = time_value.IsZero()
2013-11-02 13:29:56 +04:00
} else {
2013-11-10 14:33:37 +04:00
_, is_scanner := reflect.New(value.Type()).Interface().(sql.Scanner)
if is_scanner {
field.IsBlank = !value.FieldByName("Valid").Interface().(bool)
2013-11-10 14:33:37 +04:00
} else {
m := &Model{data: value.Interface(), driver: m.driver}
2013-11-10 14:33:37 +04:00
fields := m.columnsHasValue("other")
if len(fields) == 0 {
field.IsBlank = true
}
2013-11-02 13:29:56 +04:00
}
2013-10-29 13:37:45 +04:00
}
}
2013-10-27 10:34:34 +04:00
2013-11-02 10:12:18 +04:00
if is_time {
2013-10-29 18:00:06 +04:00
switch operation {
case "create":
2013-11-02 10:12:18 +04:00
if (field.AutoCreateTime || field.AutoUpdateTime) && time_value.IsZero() {
2013-10-29 18:00:06 +04:00
value.Set(reflect.ValueOf(time.Now()))
}
case "update":
2013-11-05 18:34:49 +04:00
if field.AutoCreateTime && time_value.IsZero() {
value.Set(reflect.ValueOf(time.Now()))
}
2013-10-29 18:00:06 +04:00
if field.AutoUpdateTime {
value.Set(reflect.ValueOf(time.Now()))
}
2013-10-27 10:34:34 +04:00
}
2013-11-10 15:38:28 +04:00
field.SqlType = getSqlType(m.driver, value, 0)
} else if field.IsPrimaryKey {
field.SqlType = getPrimaryKeySqlType(m.driver, value, 0)
2013-10-26 13:56:00 +04:00
} else {
2013-11-10 15:38:28 +04:00
field_value := reflect.Indirect(value)
2013-11-02 14:03:04 +04:00
switch field_value.Kind() {
2013-11-02 10:12:18 +04:00
case reflect.Slice:
2013-11-02 16:05:05 +04:00
foreign_key := typ.Name() + "Id"
if reflect.New(field_value.Type().Elem()).Elem().FieldByName(foreign_key).IsValid() {
field.foreignKey = foreign_key
}
2013-11-02 13:29:56 +04:00
field.afterAssociation = true
2013-11-02 10:12:18 +04:00
case reflect.Struct:
2013-11-10 15:38:28 +04:00
_, is_scanner := reflect.New(field_value.Type()).Interface().(sql.Scanner)
if is_scanner {
field.SqlType = getSqlType(m.driver, value, 0)
} else {
if indirect_value.FieldByName(p.Name + "Id").IsValid() {
field.foreignKey = p.Name + "Id"
field.beforeAssociation = true
} else {
foreign_key := typ.Name() + "Id"
if reflect.New(field_value.Type()).Elem().FieldByName(foreign_key).IsValid() {
field.foreignKey = foreign_key
2013-11-05 18:34:49 +04:00
}
field.afterAssociation = true
2013-11-02 13:29:56 +04:00
}
2013-11-02 10:12:18 +04:00
}
default:
2013-11-10 15:38:28 +04:00
field.SqlType = getSqlType(m.driver, value, 0)
2013-11-02 10:12:18 +04:00
}
2013-10-26 13:56:00 +04:00
}
2013-11-10 15:38:28 +04:00
field.Value = value.Interface()
2013-10-26 13:28:52 +04:00
fields = append(fields, field)
}
}
2013-10-29 13:37:45 +04:00
2013-11-02 11:17:11 +04:00
if len(m._cache_fields) == 0 {
m._cache_fields = make(map[string][]Field)
}
m._cache_fields[operation] = fields
return
2013-10-29 13:37:45 +04:00
}
func (m *Model) columnsHasValue(operation string) (fields []Field) {
for _, field := range m.fields(operation) {
if !field.IsBlank {
fields = append(fields, field)
}
}
return
2013-10-26 12:29:39 +04:00
}
func (m *Model) updatedColumnsAndValues(values map[string]interface{}) (map[string]interface{}, bool) {
if m.data == nil {
return values, true
}
data := reflect.Indirect(reflect.ValueOf(m.data))
results := map[string]interface{}{}
for key, value := range values {
field := data.FieldByName(snakeToUpperCamel(key))
if field.IsValid() {
if field.Interface() != value {
switch field.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
if field.Int() != reflect.ValueOf(value).Int() {
results[key] = value
}
2013-10-31 05:34:27 +04:00
field.SetInt(reflect.ValueOf(value).Int())
default:
results[key] = value
field.Set(reflect.ValueOf(value))
}
}
}
}
if values["updated_at"] != nil && len(results) > 0 {
setFieldValue(data.FieldByName("UpdatedAt"), time.Now())
}
result := len(results) > 0
return map[string]interface{}{}, result
}
2013-10-28 17:52:22 +04:00
func (m *Model) columnsAndValues(operation string) map[string]interface{} {
if m.data == nil {
return map[string]interface{}{}
}
2013-10-28 17:52:22 +04:00
results := map[string]interface{}{}
2013-10-28 11:55:41 +04:00
for _, field := range m.fields(operation) {
2013-11-02 10:12:18 +04:00
if !field.IsPrimaryKey && (len(field.SqlType) > 0) {
2013-10-28 17:52:22 +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 {
if m.data == nil {
return false
}
2013-10-29 06:19:20 +04:00
data := reflect.Indirect(reflect.ValueOf(m.data))
if data.Kind() == reflect.Slice {
2013-10-29 18:00:06 +04:00
return reflect.New(data.Type().Elem()).Elem().FieldByName(name).IsValid()
2013-10-29 06:19:20 +04:00
} else {
return data.FieldByName(name).IsValid()
}
}
func (m *Model) ColumnAndValue(name string) (has_column bool, is_slice bool, value interface{}) {
2013-10-29 03:39:26 +04:00
if m.data == nil {
2013-10-28 08:12:12 +04:00
return
}
data := reflect.Indirect(reflect.ValueOf(m.data))
if data.Kind() == reflect.Slice {
has_column = reflect.New(data.Type().Elem()).Elem().FieldByName(name).IsValid()
is_slice = true
} else {
if has_column = data.FieldByName(name).IsValid(); has_column {
value = data.FieldByName(name).Interface()
}
}
return
}
func (m *Model) typeName() string {
2013-10-29 18:00:06 +04:00
typ := reflect.Indirect(reflect.ValueOf(m.data)).Type()
if typ.Kind() == reflect.Slice {
typ = typ.Elem()
}
2013-10-28 11:49:05 +04:00
return typ.Name()
}
2013-11-10 18:29:53 +04:00
func (m *Model) tableName() (str string, err error) {
if m.data == nil {
err = errors.New("Model haven't been set")
return
}
fm := reflect.Indirect(reflect.ValueOf(m.data)).MethodByName("TableName")
if fm.IsValid() {
v := fm.Call([]reflect.Value{})
if len(v) > 0 {
if result, ok := v[0].Interface().(string); ok {
return result, nil
}
}
}
str = toSnake(m.typeName())
2013-11-06 17:43:41 +04:00
if !singularTableName {
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) {
return reg.ReplaceAllString(str, value), err
}
2013-10-28 11:49:05 +04:00
}
}
2013-10-28 08:12:12 +04:00
return
}
2013-10-27 11:24:01 +04:00
func (m *Model) callMethod(method string) error {
2013-10-29 03:39:26 +04:00
if m.data == nil {
2013-10-28 17:52:22 +04:00
return nil
}
2013-10-29 03:39:26 +04:00
fm := reflect.ValueOf(m.data).MethodByName(method)
2013-10-27 10:51:23 +04:00
if fm.IsValid() {
v := fm.Call([]reflect.Value{})
2013-10-27 11:24:01 +04:00
if len(v) > 0 {
if verr, ok := v[0].Interface().(error); ok {
return verr
}
2013-10-27 10:51:23 +04:00
}
}
return nil
}
func (m *Model) returningStr() (str string) {
if m.driver == "postgres" {
str = fmt.Sprintf("RETURNING \"%v\"", m.primaryKeyDb())
2013-10-26 16:20:49 +04:00
}
return
}
2013-10-29 03:39:26 +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-02 13:29:56 +04:00
func (m *Model) beforeAssociations() (fields []Field) {
for _, field := range m.fields("null") {
if field.beforeAssociation && !field.IsBlank {
fields = append(fields, field)
}
}
return
}
func (m *Model) afterAssociations() (fields []Field) {
for _, field := range m.fields("null") {
if field.afterAssociation && !field.IsBlank {
fields = append(fields, field)
}
}
return
}
2013-11-02 14:25:01 +04:00
func setFieldValue(field reflect.Value, value interface{}) bool {
2013-11-02 13:29:56 +04:00
if field.IsValid() && field.CanAddr() {
switch field.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
if str, ok := value.(string); ok {
value, _ = strconv.Atoi(str)
}
field.SetInt(reflect.ValueOf(value).Int())
default:
2013-11-10 14:33:37 +04:00
if scanner, ok := field.Addr().Interface().(sql.Scanner); ok {
scanner.Scan(value)
2013-11-10 04:14:41 +04:00
} else {
field.Set(reflect.ValueOf(value))
}
}
2013-11-02 14:25:01 +04:00
return true
}
2013-11-10 14:33:37 +04:00
return false
}