Skip to content

Commit

Permalink
small code cleanup
Browse files Browse the repository at this point in the history
Go programmers familier with `if err != nil {}` than `if err == nil {}`.
  • Loading branch information
methane committed Jun 13, 2024
1 parent 9b8d28e commit d34ea7c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
43 changes: 23 additions & 20 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,31 +400,34 @@ func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error)
}
// Send command
err := mc.writeCommandPacketStr(comQuery, query)
if err == nil {
// Read Result
var resLen int
resLen, err = handleOk.readResultSetHeaderPacket()
if err == nil {
rows := new(textRows)
rows.mc = mc
if err != nil {
return nil, mc.markBadConn(err)
}

if resLen == 0 {
rows.rs.done = true
// Read Result
var resLen int
resLen, err = handleOk.readResultSetHeaderPacket()
if err != nil {
return nil, mc.markBadConn(err)
}

switch err := rows.NextResultSet(); err {
case nil, io.EOF:
return rows, nil
default:
return nil, err
}
}
rows := new(textRows)
rows.mc = mc

// Columns
rows.rs.columns, err = mc.readColumns(resLen)
return rows, err
if resLen == 0 {
rows.rs.done = true

switch err := rows.NextResultSet(); err {
case nil, io.EOF:
return rows, nil
default:
return nil, err
}
}
return nil, mc.markBadConn(err)

// Columns
rows.rs.columns, err = mc.readColumns(resLen)
return rows, err
}

// Gets the value of the given MySQL System Variable
Expand Down
33 changes: 17 additions & 16 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,32 +524,33 @@ func (mc *okHandler) readResultOK() error {
}

// Result Set Header Packet
// http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::Resultset
// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_query_response.html
func (mc *okHandler) readResultSetHeaderPacket() (int, error) {
// handleOkPacket replaces both values; other cases leave the values unchanged.
mc.result.affectedRows = append(mc.result.affectedRows, 0)
mc.result.insertIds = append(mc.result.insertIds, 0)

data, err := mc.conn().readPacket()
if err == nil {
switch data[0] {

case iOK:
return 0, mc.handleOkPacket(data)
if err != nil {
return 0, err
}

case iERR:
return 0, mc.conn().handleErrorPacket(data)
switch data[0] {
case iOK:
return 0, mc.handleOkPacket(data)

case iLocalInFile:
return 0, mc.handleInFileRequest(string(data[1:]))
}
case iERR:
return 0, mc.conn().handleErrorPacket(data)

// column count
num, _, _ := readLengthEncodedInteger(data)
// ignore remaining data in the packet. see #1478.
return int(num), nil
case iLocalInFile:
return 0, mc.handleInFileRequest(string(data[1:]))
}
return 0, err

// column count
// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_query_response_text_resultset.html
num, _, _ := readLengthEncodedInteger(data)
// ignore remaining data in the packet. see #1478.
return int(num), nil
}

// Error Packet
Expand Down

0 comments on commit d34ea7c

Please sign in to comment.