mirror of https://github.com/go-gorm/gorm.git
yay, created_at, updated_at works
This commit is contained in:
parent
c3950eef38
commit
97f234e481
|
@ -3,7 +3,6 @@
|
||||||
Yet Another ORM library for Go, aims for developer friendly
|
Yet Another ORM library for Go, aims for developer friendly
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
* CreatedAt, UpdatedAt
|
|
||||||
* After/Before Save/Update/Create/Delete
|
* After/Before Save/Update/Create/Delete
|
||||||
* Soft Delete
|
* Soft Delete
|
||||||
* Better First method (First(&user, primary_key, where conditions))
|
* Better First method (First(&user, primary_key, where conditions))
|
||||||
|
|
49
model.go
49
model.go
|
@ -4,8 +4,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Model struct {
|
type Model struct {
|
||||||
|
@ -18,6 +18,9 @@ type Field struct {
|
||||||
Value interface{}
|
Value interface{}
|
||||||
SqlType string
|
SqlType string
|
||||||
DbName string
|
DbName string
|
||||||
|
AutoCreateTime bool
|
||||||
|
AutoUpdateTime bool
|
||||||
|
IsPrimaryKey bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) toModel(value interface{}) *Model {
|
func (s *Orm) toModel(value interface{}) *Model {
|
||||||
|
@ -52,7 +55,7 @@ func (m *Model) PrimaryKeyDb() string {
|
||||||
return toSnake(m.PrimaryKey())
|
return toSnake(m.PrimaryKey())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) Fields() (fields []Field) {
|
func (m *Model) Fields(operation string) (fields []Field) {
|
||||||
typ := reflect.TypeOf(m.Data).Elem()
|
typ := reflect.TypeOf(m.Data).Elem()
|
||||||
|
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
for i := 0; i < typ.NumField(); i++ {
|
||||||
|
@ -61,8 +64,27 @@ func (m *Model) Fields() (fields []Field) {
|
||||||
var field Field
|
var field Field
|
||||||
field.Name = p.Name
|
field.Name = p.Name
|
||||||
field.DbName = toSnake(p.Name)
|
field.DbName = toSnake(p.Name)
|
||||||
field.Value = reflect.ValueOf(m.Data).Elem().FieldByName(p.Name).Interface()
|
field.IsPrimaryKey = m.PrimaryKeyDb() == field.DbName
|
||||||
if 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 {
|
||||||
field.SqlType = getPrimaryKeySqlType(m.driver, field.Value, 0)
|
field.SqlType = getPrimaryKeySqlType(m.driver, field.Value, 0)
|
||||||
} else {
|
} else {
|
||||||
field.SqlType = getSqlType(m.driver, field.Value, 0)
|
field.SqlType = getSqlType(m.driver, field.Value, 0)
|
||||||
|
@ -73,18 +95,11 @@ func (m *Model) Fields() (fields []Field) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) ColumnsAndValues() (columns []string, values []interface{}) {
|
func (m *Model) ColumnsAndValues(operation string) (columns []string, values []interface{}) {
|
||||||
typ := reflect.TypeOf(m.Data).Elem()
|
for _, field := range m.Fields(operation) {
|
||||||
|
if !field.IsPrimaryKey {
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
columns = append(columns, field.DbName)
|
||||||
p := typ.Field(i)
|
values = append(values, field.Value)
|
||||||
if !p.Anonymous {
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -113,7 +128,7 @@ func (model *Model) MissingColumns() (results []string) {
|
||||||
|
|
||||||
func (model *Model) CreateTable() (sql string) {
|
func (model *Model) CreateTable() (sql string) {
|
||||||
var sqls []string
|
var sqls []string
|
||||||
for _, field := range model.Fields() {
|
for _, field := range model.Fields("null") {
|
||||||
sqls = append(sqls, field.DbName+" "+field.SqlType)
|
sqls = append(sqls, field.DbName+" "+field.SqlType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
30
orm_test.go
30
orm_test.go
|
@ -10,6 +10,8 @@ type User struct {
|
||||||
Age int64
|
Age int64
|
||||||
Birthday time.Time
|
Birthday time.Time
|
||||||
Name string
|
Name string
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -281,3 +283,31 @@ func TestOrAndNot(t *testing.T) {
|
||||||
t.Errorf("Should find three users with name 1 and 3")
|
t.Errorf("Should find three users with name 1 and 3")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreatedAtAndUpdatedAt(t *testing.T) {
|
||||||
|
name := "check_created_at_and_updated_at"
|
||||||
|
u := User{Name: name, Age: 1}
|
||||||
|
db.Save(&u)
|
||||||
|
created_at := u.CreatedAt
|
||||||
|
updated_at := u.UpdatedAt
|
||||||
|
|
||||||
|
if created_at.IsZero() {
|
||||||
|
t.Errorf("Should have created_at after create")
|
||||||
|
}
|
||||||
|
if updated_at.IsZero() {
|
||||||
|
t.Errorf("Should have updated_at after create")
|
||||||
|
}
|
||||||
|
|
||||||
|
u.Name = "check_created_at_and_updated_at_2"
|
||||||
|
db.Save(&u)
|
||||||
|
created_at2 := u.CreatedAt
|
||||||
|
updated_at2 := u.UpdatedAt
|
||||||
|
|
||||||
|
if created_at != created_at2 {
|
||||||
|
t.Errorf("Created At should not changed after update")
|
||||||
|
}
|
||||||
|
|
||||||
|
if updated_at == updated_at2 {
|
||||||
|
t.Errorf("Updated At should be changed after update")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
4
sql.go
4
sql.go
|
@ -110,7 +110,7 @@ func (s *Orm) pluck(value interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) createSql(value interface{}) {
|
func (s *Orm) createSql(value interface{}) {
|
||||||
columns, values := s.model.ColumnsAndValues()
|
columns, values := s.model.ColumnsAndValues("create")
|
||||||
|
|
||||||
var sqls []string
|
var sqls []string
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
|
@ -144,7 +144,7 @@ func (s *Orm) create(value interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Orm) updateSql(value interface{}) {
|
func (s *Orm) updateSql(value interface{}) {
|
||||||
columns, values := s.model.ColumnsAndValues()
|
columns, values := s.model.ColumnsAndValues("update")
|
||||||
var sets []string
|
var sets []string
|
||||||
for index, column := range columns {
|
for index, column := range columns {
|
||||||
sets = append(sets, fmt.Sprintf("%v = %v", s.quote(column), s.addToVars(values[index])))
|
sets = append(sets, fmt.Sprintf("%v = %v", s.quote(column), s.addToVars(values[index])))
|
||||||
|
|
Loading…
Reference in New Issue