From d53f5cf6ddf00a6cf670c5133a6dc57a080a85a7 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 13 Jan 2016 14:58:30 +0800 Subject: [PATCH] Rename scope.Trace to trace --- callback_create.go | 2 +- callback_query.go | 2 +- query_test.go | 4 +-- scope.go | 65 +++------------------------------------------- scope_private.go | 63 ++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 69 insertions(+), 67 deletions(-) diff --git a/callback_create.go b/callback_create.go index d13a71be..07121544 100644 --- a/callback_create.go +++ b/callback_create.go @@ -19,7 +19,7 @@ func UpdateTimeStampWhenCreate(scope *Scope) { } func Create(scope *Scope) { - defer scope.Trace(NowFunc()) + defer scope.trace(NowFunc()) if !scope.HasError() { // set create sql diff --git a/callback_query.go b/callback_query.go index 5473f232..75175b0d 100644 --- a/callback_query.go +++ b/callback_query.go @@ -7,7 +7,7 @@ import ( ) func Query(scope *Scope) { - defer scope.Trace(NowFunc()) + defer scope.trace(NowFunc()) var ( isSlice bool diff --git a/query_test.go b/query_test.go index 274e8e9b..71ced650 100644 --- a/query_test.go +++ b/query_test.go @@ -66,8 +66,8 @@ func TestUIntPrimaryKey(t *testing.T) { func TestStringPrimaryKeyForNumericValueStartingWithZero(t *testing.T) { type AddressByZipCode struct { - ZipCode string `gorm:"primary_key"` - Address string + ZipCode string `gorm:"primary_key"` + Address string } DB.AutoMigrate(&AddressByZipCode{}) diff --git a/scope.go b/scope.go index a11d4ec4..ef0763ce 100644 --- a/scope.go +++ b/scope.go @@ -5,7 +5,6 @@ import ( "fmt" "regexp" "strings" - "time" "reflect" ) @@ -265,7 +264,7 @@ type dbTabler interface { TableName(*DB) string } -// TableName get table name +// TableName return table name func (scope *Scope) TableName() string { if scope.Search != nil && len(scope.Search.tableName) > 0 { return scope.Search.tableName @@ -282,6 +281,7 @@ func (scope *Scope) TableName() string { return scope.GetModelStruct().TableName(scope.db.Model(scope.Value)) } +// QuotedTableName return quoted table name func (scope *Scope) QuotedTableName() (name string) { if scope.Search != nil && len(scope.Search.tableName) > 0 { if strings.Index(scope.Search.tableName, " ") != -1 { @@ -299,6 +299,7 @@ func (scope *Scope) CombinedConditionSql() string { scope.havingSql() + scope.orderSql() + scope.limitSql() + scope.offsetSql() } +// FieldByName find gorm.Field with name and db name func (scope *Scope) FieldByName(name string) (field *Field, ok bool) { for _, field := range scope.Fields() { if field.Name == name || field.DBName == name { @@ -316,7 +317,7 @@ func (scope *Scope) Raw(sql string) *Scope { // Exec invoke sql func (scope *Scope) Exec() *Scope { - defer scope.Trace(NowFunc()) + defer scope.trace(NowFunc()) if !scope.HasError() { if result, err := scope.SqlDB().Exec(scope.Sql, scope.SqlVars...); scope.Err(err) == nil { @@ -355,13 +356,6 @@ func (scope *Scope) InstanceGet(name string) (interface{}, bool) { return scope.Get(name + scope.InstanceId()) } -// Trace print sql log -func (scope *Scope) Trace(t time.Time) { - if len(scope.Sql) > 0 { - scope.db.slog(scope.Sql, t, scope.SqlVars...) - } -} - // Begin start a transaction func (scope *Scope) Begin() *Scope { if db, ok := scope.SqlDB().(sqlDb); ok { @@ -410,54 +404,3 @@ func (scope *Scope) SelectAttrs() []string { func (scope *Scope) OmitAttrs() []string { return scope.Search.omits } - -func (scope *Scope) changeableDBColumn(column string) bool { - selectAttrs := scope.SelectAttrs() - omitAttrs := scope.OmitAttrs() - - if len(selectAttrs) > 0 { - for _, attr := range selectAttrs { - if column == ToDBName(attr) { - return true - } - } - return false - } - - for _, attr := range omitAttrs { - if column == ToDBName(attr) { - return false - } - } - return true -} - -func (scope *Scope) changeableField(field *Field) bool { - selectAttrs := scope.SelectAttrs() - omitAttrs := scope.OmitAttrs() - - if len(selectAttrs) > 0 { - for _, attr := range selectAttrs { - if field.Name == attr || field.DBName == attr { - return true - } - } - return false - } - - for _, attr := range omitAttrs { - if field.Name == attr || field.DBName == attr { - return false - } - } - - return !field.IsIgnored -} - -func (scope *Scope) shouldSaveAssociations() bool { - saveAssociations, ok := scope.Get("gorm:save_associations") - if ok && !saveAssociations.(bool) { - return false - } - return true && !scope.HasError() -} diff --git a/scope_private.go b/scope_private.go index 135e7f92..a4d53366 100644 --- a/scope_private.go +++ b/scope_private.go @@ -8,6 +8,7 @@ import ( "regexp" "strconv" "strings" + "time" ) func (scope *Scope) primaryCondition(value interface{}) string { @@ -367,14 +368,14 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}, ignore } func (scope *Scope) row() *sql.Row { - defer scope.Trace(NowFunc()) + defer scope.trace(NowFunc()) scope.callCallbacks(scope.db.parent.callback.rowQueries) scope.prepareQuerySql() return scope.SqlDB().QueryRow(scope.Sql, scope.SqlVars...) } func (scope *Scope) rows() (*sql.Rows, error) { - defer scope.Trace(NowFunc()) + defer scope.trace(NowFunc()) scope.callCallbacks(scope.db.parent.callback.rowQueries) scope.prepareQuerySql() return scope.SqlDB().Query(scope.Sql, scope.SqlVars...) @@ -425,6 +426,64 @@ func (scope *Scope) typeName() string { return typ.Name() } +// trace print sql log +func (scope *Scope) trace(t time.Time) { + if len(scope.Sql) > 0 { + scope.db.slog(scope.Sql, t, scope.SqlVars...) + } +} + +func (scope *Scope) changeableDBColumn(column string) bool { + selectAttrs := scope.SelectAttrs() + omitAttrs := scope.OmitAttrs() + + if len(selectAttrs) > 0 { + for _, attr := range selectAttrs { + if column == ToDBName(attr) { + return true + } + } + return false + } + + for _, attr := range omitAttrs { + if column == ToDBName(attr) { + return false + } + } + return true +} + +func (scope *Scope) changeableField(field *Field) bool { + selectAttrs := scope.SelectAttrs() + omitAttrs := scope.OmitAttrs() + + if len(selectAttrs) > 0 { + for _, attr := range selectAttrs { + if field.Name == attr || field.DBName == attr { + return true + } + } + return false + } + + for _, attr := range omitAttrs { + if field.Name == attr || field.DBName == attr { + return false + } + } + + return !field.IsIgnored +} + +func (scope *Scope) shouldSaveAssociations() bool { + saveAssociations, ok := scope.Get("gorm:save_associations") + if ok && !saveAssociations.(bool) { + return false + } + return true && !scope.HasError() +} + func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope { toScope := scope.db.NewScope(value) fromFields := scope.Fields()