Skip to content

Commit

Permalink
Simplify textRows.readRow
Browse files Browse the repository at this point in the history
Refactor textRows.readRow to improve readability:
* simplified flow:
  * reduced maximum indent level
  * return error early
  * remove 3 'continue'
* remove one type cast
* remove one obsolete comment about readLengthEncodedString
* -6 lines of code
  • Loading branch information
dolmen committed Jun 2, 2020
1 parent 12508c8 commit d4d83cc
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions packets.go
Expand Up @@ -761,40 +761,34 @@ func (rows *textRows) readRow(dest []driver.Value) error {
}

// RowSet Packet
var n int
var isNull bool
pos := 0

for i := range dest {
// Read bytes and convert to string
dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
b, isNull, n, err := readLengthEncodedString(data[pos:])
if err != nil {
return err
}
pos += n
if err == nil {
if !isNull {
if !mc.parseTime {
switch {
case isNull:
dest[i] = nil
case mc.parseTime:
switch rows.rs.columns[i].fieldType {

case fieldTypeTimestamp, fieldTypeDateTime,
fieldTypeDate, fieldTypeNewDate:

t, err := parseDateTime(b, mc.cfg.Loc)
if err == nil {
dest[i] = t
continue
} else {
switch rows.rs.columns[i].fieldType {
case fieldTypeTimestamp, fieldTypeDateTime,
fieldTypeDate, fieldTypeNewDate:
dest[i], err = parseDateTime(
dest[i].([]byte),
mc.cfg.Loc,
)
if err == nil {
continue
}
default:
continue
}
}

} else {
dest[i] = nil
continue
// If parseDateTime failed, leave as []byte
}
fallthrough
default:
dest[i] = b
}
return err // err != nil
}

return nil
Expand Down

0 comments on commit d4d83cc

Please sign in to comment.