Skip to content

Commit

Permalink
Rename scope.Trace to trace
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jan 13, 2016
1 parent f0364a0 commit d53f5cf
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 67 deletions.
2 changes: 1 addition & 1 deletion callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Query(scope *Scope) {
defer scope.Trace(NowFunc())
defer scope.trace(NowFunc())

var (
isSlice bool
Expand Down
4 changes: 2 additions & 2 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
65 changes: 4 additions & 61 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"regexp"
"strings"
"time"

"reflect"
)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
63 changes: 61 additions & 2 deletions scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"
"strconv"
"strings"
"time"
)

func (scope *Scope) primaryCondition(value interface{}) string {
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d53f5cf

Please sign in to comment.