gorm/model.go

95 lines
1.8 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"
"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 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()
field.SqlType = getSqlType(m.driver, field.Value, 0)
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 {
columns = append(columns, toSnake(p.Name))
value := reflect.ValueOf(m.Data).Elem().FieldByName(p.Name)
values = append(values, value.Interface())
}
}
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) ColumnType(column string) (result 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
}