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) {
defer scope.Trace(NowFunc())
defer scope.trace(NowFunc())
if !scope.HasError() {
// set create sql

View File

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

View File

@ -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()
}

View File

@ -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()