Merge pull request #206 from cihangir/master

gorm: added ability to change the time.now format
This commit is contained in:
Jinzhu 2014-08-27 16:19:55 +08:00
commit 5347765bd3
10 changed files with 23 additions and 19 deletions

View File

@ -3,7 +3,6 @@ package gorm
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
) )
func BeforeCreate(scope *Scope) { func BeforeCreate(scope *Scope) {
@ -13,14 +12,14 @@ func BeforeCreate(scope *Scope) {
func UpdateTimeStampWhenCreate(scope *Scope) { func UpdateTimeStampWhenCreate(scope *Scope) {
if !scope.HasError() { if !scope.HasError() {
now := time.Now() now := NowFunc()
scope.SetColumn("CreatedAt", now) scope.SetColumn("CreatedAt", now)
scope.SetColumn("UpdatedAt", now) scope.SetColumn("UpdatedAt", now)
} }
} }
func Create(scope *Scope) { func Create(scope *Scope) {
defer scope.Trace(time.Now()) defer scope.Trace(NowFunc())
if !scope.HasError() { if !scope.HasError() {
// set create sql // set create sql

View File

@ -1,9 +1,6 @@
package gorm package gorm
import ( import "fmt"
"fmt"
"time"
)
func BeforeDelete(scope *Scope) { func BeforeDelete(scope *Scope) {
scope.CallMethod("BeforeDelete") scope.CallMethod("BeforeDelete")
@ -15,7 +12,7 @@ func Delete(scope *Scope) {
scope.Raw( scope.Raw(
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v", fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
scope.QuotedTableName(), scope.QuotedTableName(),
scope.AddToVars(time.Now()), scope.AddToVars(NowFunc()),
scope.CombinedConditionSql(), scope.CombinedConditionSql(),
)) ))
} else { } else {

View File

@ -4,11 +4,10 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
"time"
) )
func Query(scope *Scope) { func Query(scope *Scope) {
defer scope.Trace(time.Now()) defer scope.Trace(NowFunc())
var ( var (
isSlice bool isSlice bool

View File

@ -3,7 +3,6 @@ package gorm
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
) )
func AssignUpdateAttributes(scope *Scope) { func AssignUpdateAttributes(scope *Scope) {
@ -36,7 +35,7 @@ func BeforeUpdate(scope *Scope) {
func UpdateTimeStampWhenUpdate(scope *Scope) { func UpdateTimeStampWhenUpdate(scope *Scope) {
_, ok := scope.Get("gorm:update_column") _, ok := scope.Get("gorm:update_column")
if !ok { if !ok {
scope.SetColumn("UpdatedAt", time.Now()) scope.SetColumn("UpdatedAt", NowFunc())
} }
} }

View File

@ -27,7 +27,7 @@ There are four kinds of callbacks corresponds to sql's CURD: create callbacks, u
func updateCreated(scope *Scope) { func updateCreated(scope *Scope) {
if scope.HasColumn("Created") { if scope.HasColumn("Created") {
scope.SetColumn("Created", time.Now()) scope.SetColumn("Created", NowFunc())
} }
} }

View File

@ -24,7 +24,7 @@ var sqlRegexp = regexp.MustCompile(`(\$\d+)|\?`)
func (logger Logger) Print(v ...interface{}) { func (logger Logger) Print(v ...interface{}) {
if len(v) > 1 { if len(v) > 1 {
level := v[0] level := v[0]
currentTime := "\n\033[33m[" + time.Now().Format("2006-01-02 15:04:05") + "]\033[0m" currentTime := "\n\033[33m[" + NowFunc().Format("2006-01-02 15:04:05") + "]\033[0m"
source := fmt.Sprintf("\033[35m(%v)\033[0m", v[1]) source := fmt.Sprintf("\033[35m(%v)\033[0m", v[1])
messages := []interface{}{source, currentTime} messages := []interface{}{source, currentTime}

11
main.go
View File

@ -5,8 +5,19 @@ import (
"errors" "errors"
"fmt" "fmt"
"reflect" "reflect"
"time"
) )
// NowFunc returns current time, this function is exported in order to be able
// to give the flexiblity to the developer to costumize it accoring to their
// needs
//
// e.g: return time.Now().UTC()
//
var NowFunc = func() time.Time {
return time.Now()
}
type DB struct { type DB struct {
Value interface{} Value interface{}
Error error Error error

View File

@ -56,6 +56,6 @@ func (s *DB) log(v ...interface{}) {
func (s *DB) slog(sql string, t time.Time, vars ...interface{}) { func (s *DB) slog(sql string, t time.Time, vars ...interface{}) {
if s.logMode == 2 { if s.logMode == 2 {
s.print("sql", fileWithLineNum(), time.Now().Sub(t), sql, vars) s.print("sql", fileWithLineNum(), NowFunc().Sub(t), sql, vars)
} }
} }

View File

@ -349,7 +349,7 @@ func (scope *Scope) Raw(sql string) *Scope {
// Exec invoke sql // Exec invoke sql
func (scope *Scope) Exec() *Scope { func (scope *Scope) Exec() *Scope {
defer scope.Trace(time.Now()) defer scope.Trace(NowFunc())
if !scope.HasError() { if !scope.HasError() {
result, err := scope.DB().Exec(scope.Sql, scope.SqlVars...) result, err := scope.DB().Exec(scope.Sql, scope.SqlVars...)

View File

@ -9,7 +9,6 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
) )
func (scope *Scope) primaryCondiation(value interface{}) string { func (scope *Scope) primaryCondiation(value interface{}) string {
@ -368,13 +367,13 @@ func (scope *Scope) sqlTagForField(field *Field) (typ string) {
} }
func (scope *Scope) row() *sql.Row { func (scope *Scope) row() *sql.Row {
defer scope.Trace(time.Now()) defer scope.Trace(NowFunc())
scope.prepareQuerySql() scope.prepareQuerySql()
return scope.DB().QueryRow(scope.Sql, scope.SqlVars...) return scope.DB().QueryRow(scope.Sql, scope.SqlVars...)
} }
func (scope *Scope) rows() (*sql.Rows, error) { func (scope *Scope) rows() (*sql.Rows, error) {
defer scope.Trace(time.Now()) defer scope.Trace(NowFunc())
scope.prepareQuerySql() scope.prepareQuerySql()
return scope.DB().Query(scope.Sql, scope.SqlVars...) return scope.DB().Query(scope.Sql, scope.SqlVars...)
} }