gorm/model.go

159 lines
3.4 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-26 13:28:52 +04:00
Data interface{}
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-26 13:28:52 +04:00
}
2013-10-27 15:41:58 +04:00
func (m *Model) PrimaryKeyZero() bool {
2013-10-26 19:30:17 +04:00
return m.PrimaryKeyValue() == 0
}
func (m *Model) PrimaryKeyValue() int64 {
t := reflect.TypeOf(m.Data).Elem()
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
return 0
default:
result := reflect.ValueOf(m.Data).Elem()
2013-10-27 05:32:49 +04:00
value := result.FieldByName(m.PrimaryKey())
if value.IsValid() {
return result.FieldByName(m.PrimaryKey()).Interface().(int64)
} else {
return 0
}
2013-10-26 19:30:17 +04:00
}
2013-10-26 16:53:21 +04:00
}
2013-10-26 13:56:00 +04:00
func (m *Model) PrimaryKey() string {
return "Id"
}
2013-10-26 16:20:49 +04:00
func (m *Model) PrimaryKeyDb() string {
return toSnake(m.PrimaryKey())
}
2013-10-27 10:34:34 +04:00
func (m *Model) Fields(operation string) (fields []Field) {
2013-10-26 13:28:52 +04:00
typ := reflect.TypeOf(m.Data).Elem()
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-27 10:34:34 +04:00
field.IsPrimaryKey = m.PrimaryKeyDb() == field.DbName
field.AutoCreateTime = "created_at" == field.DbName
field.AutoUpdateTime = "updated_at" == field.DbName
value := reflect.ValueOf(m.Data).Elem().FieldByName(p.Name)
switch operation {
case "create":
if (field.AutoCreateTime || field.AutoUpdateTime) && value.Interface().(time.Time).IsZero() {
value = reflect.ValueOf(time.Now())
reflect.ValueOf(m.Data).Elem().FieldByName(p.Name).Set(value)
}
case "update":
if field.AutoUpdateTime {
value = reflect.ValueOf(time.Now())
reflect.ValueOf(m.Data).Elem().FieldByName(p.Name).Set(value)
}
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-26 12:29:39 +04:00
}
2013-10-27 10:34:34 +04:00
func (m *Model) ColumnsAndValues(operation string) (columns []string, values []interface{}) {
for _, field := range m.Fields(operation) {
if !field.IsPrimaryKey {
columns = append(columns, field.DbName)
values = append(values, field.Value)
2013-10-26 12:29:39 +04:00
}
}
return
}
2013-10-28 08:12:12 +04:00
func (m *Model) TableName() (str string, err error) {
if m.Data == nil {
err = errors.New("Model haven't been set")
return
}
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 {
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-26 13:28:52 +04:00
func (model *Model) MissingColumns() (results []string) {
2013-10-26 12:29:39 +04:00
return
}
2013-10-26 16:20:49 +04:00
func (model *Model) ReturningStr() (str string) {
if model.driver == "postgres" {
str = fmt.Sprintf("RETURNING \"%v\"", model.PrimaryKeyDb())
}
return
}