Fix rows affected log for Pluck, and remove rows affected log uncountable case and getting single row case

This commit is contained in:
zaneli 2019-12-08 23:55:19 +09:00
parent 79a77d771d
commit 1181263f8a
5 changed files with 26 additions and 14 deletions

View File

@ -50,7 +50,7 @@ func updateTimeStampForCreateCallback(scope *Scope) {
// createCallback the callback used to insert data into database // createCallback the callback used to insert data into database
func createCallback(scope *Scope) { func createCallback(scope *Scope) {
if !scope.HasError() { if !scope.HasError() {
defer scope.trace(NowFunc()) defer scope.trace(NowFunc(), true)
var ( var (
columns, placeholders []string columns, placeholders []string

View File

@ -24,7 +24,7 @@ func queryCallback(scope *Scope) {
return return
} }
defer scope.trace(NowFunc()) defer scope.trace(NowFunc(), true)
var ( var (
isSlice, isPtr bool isSlice, isPtr bool

View File

@ -106,7 +106,9 @@ var LogFormatter = func(values ...interface{}) (messages []interface{}) {
} }
messages = append(messages, sql) messages = append(messages, sql)
messages = append(messages, fmt.Sprintf(" \n\033[36;31m[%v]\033[0m ", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned ")) if len(values) > 5 {
messages = append(messages, fmt.Sprintf(" \n\033[36;31m[%v]\033[0m ", strconv.FormatInt(values[5].(int64), 10)+" rows affected or returned "))
}
} else { } else {
messages = append(messages, "\033[31;1m") messages = append(messages, "\033[31;1m")
messages = append(messages, values[2:]...) messages = append(messages, values[2:]...)

16
main.go
View File

@ -366,12 +366,16 @@ func (s *DB) Scan(dest interface{}) *DB {
// Row return `*sql.Row` with given conditions // Row return `*sql.Row` with given conditions
func (s *DB) Row() *sql.Row { func (s *DB) Row() *sql.Row {
return s.NewScope(s.Value).row() scope := s.NewScope(s.Value)
defer scope.trace(NowFunc(), false)
return scope.row()
} }
// Rows return `*sql.Rows` with given conditions // Rows return `*sql.Rows` with given conditions
func (s *DB) Rows() (*sql.Rows, error) { func (s *DB) Rows() (*sql.Rows, error) {
return s.NewScope(s.Value).rows() scope := s.NewScope(s.Value)
defer scope.trace(NowFunc(), false)
return scope.rows()
} }
// ScanRows scan `*sql.Rows` to give struct // ScanRows scan `*sql.Rows` to give struct
@ -874,8 +878,12 @@ func (s *DB) log(v ...interface{}) {
} }
} }
func (s *DB) slog(sql string, t time.Time, vars ...interface{}) { func (s *DB) slog(sql string, t time.Time, showRowsAffected bool, vars ...interface{}) {
if s.logMode == detailedLogMode { if s.logMode == detailedLogMode {
s.print("sql", fileWithLineNum(), NowFunc().Sub(t), sql, vars, s.RowsAffected) if showRowsAffected {
s.print("sql", fileWithLineNum(), NowFunc().Sub(t), sql, vars, s.RowsAffected)
} else {
s.print("sql", fileWithLineNum(), NowFunc().Sub(t), sql, vars)
}
} }
} }

View File

@ -358,7 +358,7 @@ func (scope *Scope) Raw(sql string) *Scope {
// Exec perform generated SQL // Exec perform generated SQL
func (scope *Scope) Exec() *Scope { func (scope *Scope) Exec() *Scope {
defer scope.trace(NowFunc()) defer scope.trace(NowFunc(), true)
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 {
@ -934,8 +934,6 @@ func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[strin
} }
func (scope *Scope) row() *sql.Row { func (scope *Scope) row() *sql.Row {
defer scope.trace(NowFunc())
result := &RowQueryResult{} result := &RowQueryResult{}
scope.InstanceSet("row_query_result", result) scope.InstanceSet("row_query_result", result)
scope.callCallbacks(scope.db.parent.callbacks.rowQueries) scope.callCallbacks(scope.db.parent.callbacks.rowQueries)
@ -944,8 +942,6 @@ func (scope *Scope) row() *sql.Row {
} }
func (scope *Scope) rows() (*sql.Rows, error) { func (scope *Scope) rows() (*sql.Rows, error) {
defer scope.trace(NowFunc())
result := &RowsQueryResult{} result := &RowsQueryResult{}
scope.InstanceSet("row_query_result", result) scope.InstanceSet("row_query_result", result)
scope.callCallbacks(scope.db.parent.callbacks.rowQueries) scope.callCallbacks(scope.db.parent.callbacks.rowQueries)
@ -980,6 +976,8 @@ func (scope *Scope) isQueryForColumn(query interface{}, column string) bool {
} }
func (scope *Scope) pluck(column string, value interface{}) *Scope { func (scope *Scope) pluck(column string, value interface{}) *Scope {
defer scope.trace(NowFunc(), true)
dest := reflect.Indirect(reflect.ValueOf(value)) dest := reflect.Indirect(reflect.ValueOf(value))
if dest.Kind() != reflect.Slice { if dest.Kind() != reflect.Slice {
scope.Err(fmt.Errorf("results should be a slice, not %s", dest.Kind())) scope.Err(fmt.Errorf("results should be a slice, not %s", dest.Kind()))
@ -998,6 +996,8 @@ func (scope *Scope) pluck(column string, value interface{}) *Scope {
if scope.Err(err) == nil { if scope.Err(err) == nil {
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
scope.db.RowsAffected++
elem := reflect.New(dest.Type().Elem()).Interface() elem := reflect.New(dest.Type().Elem()).Interface()
scope.Err(rows.Scan(elem)) scope.Err(rows.Scan(elem))
dest.Set(reflect.Append(dest, reflect.ValueOf(elem).Elem())) dest.Set(reflect.Append(dest, reflect.ValueOf(elem).Elem()))
@ -1011,6 +1011,8 @@ func (scope *Scope) pluck(column string, value interface{}) *Scope {
} }
func (scope *Scope) count(value interface{}) *Scope { func (scope *Scope) count(value interface{}) *Scope {
defer scope.trace(NowFunc(), false)
if query, ok := scope.Search.selects["query"]; !ok || !countingQueryRegexp.MatchString(fmt.Sprint(query)) { if query, ok := scope.Search.selects["query"]; !ok || !countingQueryRegexp.MatchString(fmt.Sprint(query)) {
if len(scope.Search.group) != 0 { if len(scope.Search.group) != 0 {
if len(scope.Search.havingConditions) != 0 { if len(scope.Search.havingConditions) != 0 {
@ -1042,9 +1044,9 @@ func (scope *Scope) typeName() string {
} }
// trace print sql log // trace print sql log
func (scope *Scope) trace(t time.Time) { func (scope *Scope) trace(t time.Time, showRowsAffected bool) {
if len(scope.SQL) > 0 { if len(scope.SQL) > 0 {
scope.db.slog(scope.SQL, t, scope.SQLVars...) scope.db.slog(scope.SQL, t, showRowsAffected, scope.SQLVars...)
} }
} }