gorm/model.go

129 lines
2.6 KiB
Go
Raw Normal View History

2013-10-26 12:29:39 +04:00
package gorm
import (
2013-10-26 13:28:52 +04:00
"fmt"
"reflect"
2013-10-26 13:28:52 +04:00
"regexp"
2013-10-26 16:53:21 +04:00
2013-10-26 13:28:52 +04:00
"strings"
)
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 {
Name string
Value interface{}
SqlType string
DbName string
}
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()
return result.FieldByName(m.PrimaryKey()).Interface().(int64)
}
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-26 13:28:52 +04:00
func (m *Model) Fields() (fields []Field) {
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)
field.Value = reflect.ValueOf(m.Data).Elem().FieldByName(p.Name).Interface()
2013-10-26 16:20:49 +04:00
if m.PrimaryKeyDb() == field.DbName {
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
}
func (m *Model) ColumnsAndValues() (columns []string, values []interface{}) {
typ := reflect.TypeOf(m.Data).Elem()
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
2013-10-26 16:20:49 +04:00
db_name := toSnake(p.Name)
if m.PrimaryKeyDb() != db_name {
columns = append(columns, db_name)
value := reflect.ValueOf(m.Data).Elem().FieldByName(p.Name)
values = append(values, value.Interface())
}
2013-10-26 12:29:39 +04:00
}
}
return
}
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-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
for _, field := range model.Fields() {
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
}