Merge pull request 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 (
"fmt"
"strings"
"time"
)
func BeforeCreate(scope *Scope) {
@ -13,14 +12,14 @@ func BeforeCreate(scope *Scope) {
func UpdateTimeStampWhenCreate(scope *Scope) {
if !scope.HasError() {
now := time.Now()
now := NowFunc()
scope.SetColumn("CreatedAt", now)
scope.SetColumn("UpdatedAt", now)
}
}
func Create(scope *Scope) {
defer scope.Trace(time.Now())
defer scope.Trace(NowFunc())
if !scope.HasError() {
// set create sql

View File

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

View File

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

View File

@ -3,7 +3,6 @@ package gorm
import (
"fmt"
"strings"
"time"
)
func AssignUpdateAttributes(scope *Scope) {
@ -36,7 +35,7 @@ func BeforeUpdate(scope *Scope) {
func UpdateTimeStampWhenUpdate(scope *Scope) {
_, ok := scope.Get("gorm:update_column")
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) {
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{}) {
if len(v) > 1 {
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])
messages := []interface{}{source, currentTime}

11
main.go
View File

@ -5,8 +5,19 @@ import (
"errors"
"fmt"
"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 {
Value interface{}
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{}) {
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
func (scope *Scope) Exec() *Scope {
defer scope.Trace(time.Now())
defer scope.Trace(NowFunc())
if !scope.HasError() {
result, err := scope.DB().Exec(scope.Sql, scope.SqlVars...)

View File

@ -9,7 +9,6 @@ import (
"regexp"
"strconv"
"strings"
"time"
)
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 {
defer scope.Trace(time.Now())
defer scope.Trace(NowFunc())
scope.prepareQuerySql()
return scope.DB().QueryRow(scope.Sql, scope.SqlVars...)
}
func (scope *Scope) rows() (*sql.Rows, error) {
defer scope.Trace(time.Now())
defer scope.Trace(NowFunc())
scope.prepareQuerySql()
return scope.DB().Query(scope.Sql, scope.SqlVars...)
}