Skip to content

Commit

Permalink
defer getWarnings() after fetching resultsets.
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed Jun 1, 2017
1 parent e3f0fdc commit 7a1a598
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type mysqlConn struct {
sequence uint8
parseTime bool
strict bool
warningCount uint16
}

// Handles parameters set in DSN after the connection is established
Expand Down
4 changes: 4 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ func handleAuthResult(mc *mysqlConn, oldCipher []byte) error {
}
_, err = mc.readResultOK()
}

if err == nil && mc.strict && mc.warningCount > 0 {
return mc.getWarnings()
}
return err
}

Expand Down
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type MySQLWarning struct {
}

func (mc *mysqlConn) getWarnings() (err error) {
mc.warningCount = 0
rows, err := mc.Query("SHOW WARNINGS", nil)
if err != nil {
return
Expand Down
3 changes: 3 additions & 0 deletions infile.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
// read OK packet
if err == nil {
_, err = mc.readResultOK()
if err == nil && mc.strict && mc.warningCount > 0 {
err = mc.getWarnings()
}
return err
}

Expand Down
12 changes: 12 additions & 0 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,12 @@ func (mc *mysqlConn) handleOkPacket(data []byte) error {

// warning count [2 bytes]
if !mc.strict {
mc.warningCount = 0
return nil
}

pos := 1 + n + m + 2
mc.warningCount = binary.LittleEndian.Uint16(data[pos : pos+2])
if binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
return mc.getWarnings()
}
Expand Down Expand Up @@ -729,7 +731,14 @@ func (rows *textRows) readRow(dest []driver.Value) error {
rows.mc.status = readStatus(data[3:])
rows.rs.done = true
if !rows.HasNextResultSet() {
mc := rows.mc
rows.mc = nil

if mc.strict && mc.warningCount > 0 {
if err := mc.getWarnings(); err != nil {
return err
}
}
}
return io.EOF
}
Expand Down Expand Up @@ -1115,6 +1124,9 @@ func (mc *mysqlConn) discardResults() error {
}
}
}
if mc.strict && mc.warningCount > 0 {
return mc.getWarnings()
}
return nil
}

Expand Down

0 comments on commit 7a1a598

Please sign in to comment.