Skip to content

Commit

Permalink
fix: 修复mysql 5.6和mariadb获取不到受影响行数问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hanchuanchuan committed Apr 23, 2019
1 parent 1dc43c4 commit b4132fa
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions session/session_inception.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package session

import (
"bytes"
Sql "database/sql"
// Sql "database/sql"
"database/sql/driver"
"fmt"
// "io"
Expand Down Expand Up @@ -4166,41 +4166,58 @@ func (s *session) getExplainInfo(sql string, sqlId string) {
Buf: new(bytes.Buffer),
}
}
r := s.myRecord

// rows, err := s.db.DB().Query(sql)
rows, err := s.Raw(sql)
// rows, err := s.Raw(sql)

var rowLength Sql.NullInt64
// var rowLength Sql.NullInt64

if err != nil {
// if err != nil {
// log.Error(err)
// if myErr, ok := err.(*mysqlDriver.MySQLError); ok {
// s.AppendErrorMessage(myErr.Message)
// if newRecord != nil {
// newRecord.AppendErrorMessage(myErr.Message)
// }
// }
// } else {
// for rows.Next() {
// var str Sql.NullString
// // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra
// if err := rows.Scan(&str, &str, &str, &str, &str, &str, &str, &str, &str, &rowLength, &str, &str); err != nil {
// log.Error(err)
// if myErr, ok := err.(*mysqlDriver.MySQLError); ok {
// s.AppendErrorMessage(myErr.Message)
// if newRecord != nil {
// newRecord.AppendErrorMessage(myErr.Message)
// }
// }
// }
// break
// }
// rows.Close()
// }

// if rowLength.Valid {
// r.AffectedRows = int(rowLength.Int64)
// if newRecord != nil {
// newRecord.AffectedRows = r.AffectedRows
// }
// }

var rows []ExplainInfo
if err := s.db.Raw(sql).Scan(&rows).Error; err != nil {
log.Error(err)
if myErr, ok := err.(*mysqlDriver.MySQLError); ok {
s.AppendErrorMessage(myErr.Message)
if newRecord != nil {
newRecord.AppendErrorMessage(myErr.Message)
}
}
} else {
for rows.Next() {
var str Sql.NullString
// | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra
if err := rows.Scan(&str, &str, &str, &str, &str, &str, &str, &str, &str, &rowLength, &str, &str); err != nil {
log.Error(err)
if myErr, ok := err.(*mysqlDriver.MySQLError); ok {
s.AppendErrorMessage(myErr.Message)
if newRecord != nil {
newRecord.AppendErrorMessage(myErr.Message)
}
}
}
break
}
rows.Close()
}

r := s.myRecord
if rowLength.Valid {
r.AffectedRows = int(rowLength.Int64)
if len(rows) > 0 {
r.AffectedRows = rows[0].Rows
if newRecord != nil {
newRecord.AffectedRows = r.AffectedRows
}
Expand All @@ -4221,17 +4238,6 @@ func (s *session) getExplainInfo(sql string, sqlId string) {
if newRecord != nil {
s.sqlFingerprint[sqlId] = newRecord
}

// var rows []ExplainInfo
// if err := s.db.Raw(sql).Scan(&rows).Error; err != nil {
// log.Error(err)
// if myErr, ok := err.(*mysqlDriver.MySQLError); ok {
// s.AppendErrorMessage(myErr.Message)
// } else {
// s.AppendErrorMessage(err.Error())
// }
// }
// return rows
}

func (s *session) explainOrAnalyzeSql(sql string) {
Expand Down

0 comments on commit b4132fa

Please sign in to comment.