Rename scope.Trace to trace

This commit is contained in:
Jinzhu 2016-01-13 14:58:30 +08:00
parent f0364a0fb5
commit d53f5cf6dd
5 changed files with 69 additions and 67 deletions

View File

@ -19,7 +19,7 @@ func UpdateTimeStampWhenCreate(scope *Scope) {
} }
func Create(scope *Scope) { func Create(scope *Scope) {
defer scope.Trace(NowFunc()) defer scope.trace(NowFunc())
if !scope.HasError() { if !scope.HasError() {
// set create sql // set create sql

View File

@ -7,7 +7,7 @@ import (
) )
func Query(scope *Scope) { func Query(scope *Scope) {
defer scope.Trace(NowFunc()) defer scope.trace(NowFunc())
var ( var (
isSlice bool isSlice bool

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"strings" "strings"
"time"
"reflect" "reflect"
) )
@ -265,7 +264,7 @@ type dbTabler interface {
TableName(*DB) string TableName(*DB) string
} }
// TableName get table name // TableName return table name
func (scope *Scope) TableName() string { func (scope *Scope) TableName() string {
if scope.Search != nil && len(scope.Search.tableName) > 0 { if scope.Search != nil && len(scope.Search.tableName) > 0 {
return scope.Search.tableName return scope.Search.tableName
@ -282,6 +281,7 @@ func (scope *Scope) TableName() string {
return scope.GetModelStruct().TableName(scope.db.Model(scope.Value)) return scope.GetModelStruct().TableName(scope.db.Model(scope.Value))
} }
// QuotedTableName return quoted table name
func (scope *Scope) QuotedTableName() (name string) { func (scope *Scope) QuotedTableName() (name string) {
if scope.Search != nil && len(scope.Search.tableName) > 0 { if scope.Search != nil && len(scope.Search.tableName) > 0 {
if strings.Index(scope.Search.tableName, " ") != -1 { if strings.Index(scope.Search.tableName, " ") != -1 {
@ -299,6 +299,7 @@ func (scope *Scope) CombinedConditionSql() string {
scope.havingSql() + scope.orderSql() + scope.limitSql() + scope.offsetSql() 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) { func (scope *Scope) FieldByName(name string) (field *Field, ok bool) {
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
if field.Name == name || field.DBName == name { if field.Name == name || field.DBName == name {
@ -316,7 +317,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(NowFunc()) defer scope.trace(NowFunc())
if !scope.HasError() { if !scope.HasError() {
if result, err := scope.SqlDB().Exec(scope.Sql, scope.SqlVars...); scope.Err(err) == nil { 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()) 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 // Begin start a transaction
func (scope *Scope) Begin() *Scope { func (scope *Scope) Begin() *Scope {
if db, ok := scope.SqlDB().(sqlDb); ok { if db, ok := scope.SqlDB().(sqlDb); ok {
@ -410,54 +404,3 @@ func (scope *Scope) SelectAttrs() []string {
func (scope *Scope) OmitAttrs() []string { func (scope *Scope) OmitAttrs() []string {
return scope.Search.omits 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()
}

View File

@ -8,6 +8,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
) )
func (scope *Scope) primaryCondition(value interface{}) string { 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 { func (scope *Scope) row() *sql.Row {
defer scope.Trace(NowFunc()) defer scope.trace(NowFunc())
scope.callCallbacks(scope.db.parent.callback.rowQueries) scope.callCallbacks(scope.db.parent.callback.rowQueries)
scope.prepareQuerySql() scope.prepareQuerySql()
return scope.SqlDB().QueryRow(scope.Sql, scope.SqlVars...) return scope.SqlDB().QueryRow(scope.Sql, scope.SqlVars...)
} }
func (scope *Scope) rows() (*sql.Rows, error) { func (scope *Scope) rows() (*sql.Rows, error) {
defer scope.Trace(NowFunc()) defer scope.trace(NowFunc())
scope.callCallbacks(scope.db.parent.callback.rowQueries) scope.callCallbacks(scope.db.parent.callback.rowQueries)
scope.prepareQuerySql() scope.prepareQuerySql()
return scope.SqlDB().Query(scope.Sql, scope.SqlVars...) return scope.SqlDB().Query(scope.Sql, scope.SqlVars...)
@ -425,6 +426,64 @@ func (scope *Scope) typeName() string {
return typ.Name() 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 { func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
toScope := scope.db.NewScope(value) toScope := scope.db.NewScope(value)
fromFields := scope.Fields() fromFields := scope.Fields()