add type created_at deleted_at update_at
This commit is contained in:
parent
c3bb6aaa82
commit
910f753910
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -3,6 +3,7 @@ package gorm
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Define callbacks for creating
|
// Define callbacks for creating
|
||||||
@ -35,13 +36,33 @@ func updateTimeStampForCreateCallback(scope *Scope) {
|
|||||||
|
|
||||||
if createdAtField, ok := scope.FieldByName("CreatedAt"); ok {
|
if createdAtField, ok := scope.FieldByName("CreatedAt"); ok {
|
||||||
if createdAtField.IsBlank {
|
if createdAtField.IsBlank {
|
||||||
createdAtField.Set(now)
|
switch createdAtField.Field.Type().Kind() {
|
||||||
|
case reflect.Int64:
|
||||||
|
createdAtField.Set(now.Unix())
|
||||||
|
break
|
||||||
|
case reflect.String:
|
||||||
|
createdAtField.Set(now.String())
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
createdAtField.Set(now)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if updatedAtField, ok := scope.FieldByName("UpdatedAt"); ok {
|
if updatedAtField, ok := scope.FieldByName("UpdatedAt"); ok {
|
||||||
if updatedAtField.IsBlank {
|
if updatedAtField.IsBlank {
|
||||||
updatedAtField.Set(now)
|
switch updatedAtField.Field.Type().Kind() {
|
||||||
|
case reflect.Int64:
|
||||||
|
updatedAtField.Set(now.Unix())
|
||||||
|
break
|
||||||
|
case reflect.String:
|
||||||
|
updatedAtField.Set(now.String())
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
updatedAtField.Set(now)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package gorm
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Define callbacks for deleting
|
// Define callbacks for deleting
|
||||||
@ -36,11 +37,23 @@ func deleteCallback(scope *Scope) {
|
|||||||
deletedAtField, hasDeletedAtField := scope.FieldByName("DeletedAt")
|
deletedAtField, hasDeletedAtField := scope.FieldByName("DeletedAt")
|
||||||
|
|
||||||
if !scope.Search.Unscoped && hasDeletedAtField {
|
if !scope.Search.Unscoped && hasDeletedAtField {
|
||||||
|
var time interface{}
|
||||||
|
switch deletedAtField.Field.Type().Kind() {
|
||||||
|
case reflect.Int64:
|
||||||
|
time = NowFunc().Unix()
|
||||||
|
break
|
||||||
|
case reflect.String:
|
||||||
|
time = NowFunc().String()
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
time = NowFunc()
|
||||||
|
break
|
||||||
|
}
|
||||||
scope.Raw(fmt.Sprintf(
|
scope.Raw(fmt.Sprintf(
|
||||||
"UPDATE %v SET %v=%v%v%v",
|
"UPDATE %v SET %v=%v%v%v",
|
||||||
scope.QuotedTableName(),
|
scope.QuotedTableName(),
|
||||||
scope.Quote(deletedAtField.DBName),
|
scope.Quote(deletedAtField.DBName),
|
||||||
scope.AddToVars(NowFunc()),
|
scope.AddToVars(time),
|
||||||
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
||||||
addExtraSpaceIfExist(extraOption),
|
addExtraSpaceIfExist(extraOption),
|
||||||
)).Exec()
|
)).Exec()
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Define callbacks for updating
|
// Define callbacks for updating
|
||||||
@ -48,8 +49,22 @@ func beforeUpdateCallback(scope *Scope) {
|
|||||||
|
|
||||||
// updateTimeStampForUpdateCallback will set `UpdatedAt` when updating
|
// updateTimeStampForUpdateCallback will set `UpdatedAt` when updating
|
||||||
func updateTimeStampForUpdateCallback(scope *Scope) {
|
func updateTimeStampForUpdateCallback(scope *Scope) {
|
||||||
|
var time interface{}
|
||||||
|
if createdAtField, ok := scope.FieldByName("UpdatedAt"); ok {
|
||||||
|
switch createdAtField.Field.Type().Kind() {
|
||||||
|
case reflect.Int64:
|
||||||
|
time = NowFunc().Unix()
|
||||||
|
break
|
||||||
|
case reflect.String:
|
||||||
|
time = NowFunc().String()
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
time = NowFunc()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
if _, ok := scope.Get("gorm:update_column"); !ok {
|
if _, ok := scope.Get("gorm:update_column"); !ok {
|
||||||
scope.SetColumn("UpdatedAt", NowFunc())
|
scope.SetColumn("UpdatedAt", time)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
model.go
14
model.go
@ -12,3 +12,17 @@ type Model struct {
|
|||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
DeletedAt *time.Time `sql:"index"`
|
DeletedAt *time.Time `sql:"index"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ModelInt64 struct {
|
||||||
|
ID uint `gorm:"primary_key"`
|
||||||
|
UpdatedAt int64
|
||||||
|
CreatedAt int64
|
||||||
|
DeletedAt int64 `sql:"index"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ModelString struct {
|
||||||
|
ID uint `gorm:"primary_key"`
|
||||||
|
UpdatedAt string
|
||||||
|
CreatedAt string
|
||||||
|
DeletedAt string `sql:"index"`
|
||||||
|
}
|
||||||
|
13
scope.go
13
scope.go
@ -687,8 +687,19 @@ func (scope *Scope) whereSQL() (sql string) {
|
|||||||
primaryConditions, andConditions, orConditions []string
|
primaryConditions, andConditions, orConditions []string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
switch deletedAtField.Field.Type().Kind() {
|
||||||
|
case reflect.Int64:
|
||||||
|
sql = fmt.Sprintf("%v.%v = 0", quotedTableName, scope.Quote(deletedAtField.DBName))
|
||||||
|
break
|
||||||
|
case reflect.String:
|
||||||
|
sql = fmt.Sprintf("%v.%v = ''", quotedTableName, scope.Quote(deletedAtField.DBName))
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
sql = fmt.Sprintf("%v.%v IS NULL", quotedTableName, scope.Quote(deletedAtField.DBName))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
if !scope.Search.Unscoped && hasDeletedAtField {
|
if !scope.Search.Unscoped && hasDeletedAtField {
|
||||||
sql := fmt.Sprintf("%v.%v IS NULL", quotedTableName, scope.Quote(deletedAtField.DBName))
|
|
||||||
primaryConditions = append(primaryConditions, sql)
|
primaryConditions = append(primaryConditions, sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user