2013-10-26 12:29:39 +04:00
|
|
|
package gorm
|
|
|
|
|
2013-10-26 12:33:59 +04:00
|
|
|
import (
|
2013-10-26 13:28:52 +04:00
|
|
|
"fmt"
|
2013-10-26 12:33:59 +04:00
|
|
|
"reflect"
|
2013-10-26 13:28:52 +04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2013-10-27 10:34:34 +04:00
|
|
|
"time"
|
2013-10-26 12:33:59 +04:00
|
|
|
)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Orm) toModel(value interface{}) *Model {
|
|
|
|
return &Model{Data: value, driver: s.driver}
|
2013-10-26 12:29:39 +04:00
|
|
|
}
|
|
|
|
|
2013-10-26 16:53:21 +04:00
|
|
|
func (m *Model) PrimaryKeyIsEmpty() 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-26 12:33:59 +04:00
|
|
|
func (m *Model) TableName() string {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reg, _ := regexp.Compile("s*$")
|
|
|
|
return reg.ReplaceAllString(toSnake(t.Name()), "s")
|
|
|
|
}
|
|
|
|
|
2013-10-27 10:51:23 +04:00
|
|
|
func (model *Model) callMethod(method string) error {
|
|
|
|
fm := reflect.ValueOf(model).MethodByName(method)
|
|
|
|
if fm.IsValid() {
|
|
|
|
v := fm.Call([]reflect.Value{})
|
|
|
|
if verr, ok := v[0].Interface().(error); ok {
|
|
|
|
return verr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 13:28:52 +04:00
|
|
|
func (model *Model) CreateTable() (sql string) {
|
|
|
|
var sqls []string
|
2013-10-27 10:34:34 +04:00
|
|
|
for _, field := range model.Fields("null") {
|
2013-10-26 13:28:52 +04:00
|
|
|
sqls = append(sqls, field.DbName+" "+field.SqlType)
|
|
|
|
}
|
|
|
|
|
|
|
|
sql = fmt.Sprintf(
|
|
|
|
"CREATE TABLE \"%v\" (%v)",
|
|
|
|
model.TableName(),
|
|
|
|
strings.Join(sqls, ","),
|
|
|
|
)
|
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
|
|
|
|
}
|