gorm/model.go

201 lines
4.1 KiB
Go
Raw Normal View History

2013-10-26 12:29:39 +04:00
package gorm
import (
2013-10-28 08:12:12 +04:00
"errors"
2013-10-26 13:28:52 +04:00
"fmt"
"reflect"
2013-10-26 13:28:52 +04:00
"regexp"
2013-10-28 08:12:12 +04:00
2013-10-27 10:34:34 +04:00
"time"
)
2013-10-26 12:29:39 +04:00
type Model struct {
2013-10-29 03:39:26 +04:00
data interface{}
2013-10-26 13:28:52 +04:00
driver string
}
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-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-10-29 03:39:26 +04:00
t := reflect.TypeOf(m.data).Elem()
2013-10-26 19:30:17 +04:00
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
return 0
default:
2013-10-29 03:39:26 +04:00
result := reflect.ValueOf(m.data).Elem()
2013-10-28 11:55:41 +04:00
value := result.FieldByName(m.primaryKey())
2013-10-27 05:32:49 +04:00
if value.IsValid() {
2013-10-29 03:39:26 +04:00
return value.Interface().(int64)
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-10-29 13:37:45 +04:00
indirect_value := reflect.Indirect(reflect.ValueOf(m.data))
typ := indirect_value.Type()
2013-10-26 13:28:52 +04:00
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
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)
switch value.Kind() {
case reflect.Int, reflect.Int64, reflect.Int32:
field.IsBlank = value.Int() == 0
case reflect.String:
field.IsBlank = value.String() == ""
default:
if value, ok := value.Interface().(time.Time); ok {
field.IsBlank = value.IsZero()
}
}
2013-10-27 10:34:34 +04:00
switch operation {
case "create":
if (field.AutoCreateTime || field.AutoUpdateTime) && value.Interface().(time.Time).IsZero() {
2013-10-29 03:39:26 +04:00
value.Set(reflect.ValueOf(time.Now()))
2013-10-27 10:34:34 +04:00
}
case "update":
if field.AutoUpdateTime {
2013-10-29 03:39:26 +04:00
value.Set(reflect.ValueOf(time.Now()))
2013-10-27 10:34:34 +04:00
}
default:
}
field.Value = value.Interface()
if field.IsPrimaryKey {
2013-10-26 13:56:00 +04:00
field.SqlType = getPrimaryKeySqlType(m.driver, field.Value, 0)
} else {
field.SqlType = getSqlType(m.driver, field.Value, 0)
}
2013-10-26 13:28:52 +04:00
fields = append(fields, field)
}
}
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
}
2013-10-28 17:52:22 +04:00
func (m *Model) columnsAndValues(operation string) map[string]interface{} {
results := map[string]interface{}{}
2013-10-28 11:55:41 +04:00
for _, field := range m.fields(operation) {
2013-10-27 10:34:34 +04:00
if !field.IsPrimaryKey {
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 {
return false //FIXME data.Elem().FieldByName(name).IsValid()
} else {
return data.FieldByName(name).IsValid()
}
}
2013-10-28 11:55:41 +04:00
func (m *Model) tableName() (str string, err error) {
2013-10-29 03:39:26 +04:00
if m.data == nil {
2013-10-28 08:12:12 +04:00
err = errors.New("Model haven't been set")
return
}
2013-10-29 03:39:26 +04:00
t := reflect.TypeOf(m.data)
for {
c := false
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
t = t.Elem()
c = true
}
if !c {
break
}
}
2013-10-28 11:49:05 +04:00
str = toSnake(t.Name())
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 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
}
2013-10-28 11:55:41 +04:00
func (model *Model) returningStr() (str string) {
2013-10-26 16:20:49 +04:00
if model.driver == "postgres" {
2013-10-28 11:55:41 +04:00
str = fmt.Sprintf("RETURNING \"%v\"", model.primaryKeyDb())
2013-10-26 16:20:49 +04:00
}
return
}
2013-10-29 03:39:26 +04:00
func (model *Model) missingColumns() (results []string) {
return
}