diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/callback_create.go b/callback_create.go index a4da39e8..76d62a31 100644 --- a/callback_create.go +++ b/callback_create.go @@ -3,6 +3,7 @@ package gorm import ( "fmt" "strings" + "reflect" ) // Define callbacks for creating @@ -35,13 +36,33 @@ func updateTimeStampForCreateCallback(scope *Scope) { if createdAtField, ok := scope.FieldByName("CreatedAt"); ok { 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.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 + } } } } diff --git a/callback_delete.go b/callback_delete.go index 73d90880..f732f4dd 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -3,6 +3,7 @@ package gorm import ( "errors" "fmt" + "reflect" ) // Define callbacks for deleting @@ -36,11 +37,23 @@ func deleteCallback(scope *Scope) { deletedAtField, hasDeletedAtField := scope.FieldByName("DeletedAt") 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( "UPDATE %v SET %v=%v%v%v", scope.QuotedTableName(), scope.Quote(deletedAtField.DBName), - scope.AddToVars(NowFunc()), + scope.AddToVars(time), addExtraSpaceIfExist(scope.CombinedConditionSql()), addExtraSpaceIfExist(extraOption), )).Exec() diff --git a/callback_update.go b/callback_update.go index 6948439f..d4ef773b 100644 --- a/callback_update.go +++ b/callback_update.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strings" + "reflect" ) // Define callbacks for updating @@ -48,8 +49,22 @@ func beforeUpdateCallback(scope *Scope) { // updateTimeStampForUpdateCallback will set `UpdatedAt` when updating 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 { - scope.SetColumn("UpdatedAt", NowFunc()) + scope.SetColumn("UpdatedAt", time) } } diff --git a/model.go b/model.go index f37ff7ea..5e4c284a 100644 --- a/model.go +++ b/model.go @@ -12,3 +12,17 @@ type Model struct { UpdatedAt time.Time 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"` +} diff --git a/scope.go b/scope.go index fda7f653..fba35b1d 100644 --- a/scope.go +++ b/scope.go @@ -687,8 +687,19 @@ func (scope *Scope) whereSQL() (sql 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 { - sql := fmt.Sprintf("%v.%v IS NULL", quotedTableName, scope.Quote(deletedAtField.DBName)) primaryConditions = append(primaryConditions, sql) }