From 58f6c4a7a9971151a3b6e0df3ae4f829106624ed Mon Sep 17 00:00:00 2001 From: zihengCat Date: Sun, 11 Jul 2021 23:07:41 +0800 Subject: [PATCH] refactoring (*textRows).readRow in a more clear way Fix error returns use utf8mb4 instead of utf8 in TestCharset (#1228) From MySQL 8.0.24, `SELECT @@character_set_connection` reports utf8mb3 or utf8mb4 instead of utf8. Because utf8 is currently an alias for utf8mb3, however at some point utf8 is expected to become a reference to utf8mb4. > ref. https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-24.html#mysqld-8-0-24-bug > Important Note: When a utf8mb3 collation was specified in a CREATE TABLE statement, SHOW CREATE TABLE, DEFAULT CHARSET, > the values of system variables containing character set names, > and the binary log all subsequently displayed the character set as utf8 which is becoming a synonym for utf8mb4. > Now in such cases, utf8mb3 is shown instead, and CREATE TABLE raises the warning 'collation_name' is a collation of the deprecated character set UTF8MB3. > Please consider using UTF8MB4 with an appropriate collation instead. (Bug #27225287, Bug #32085357, Bug #32122844) > > References: See also: Bug #30624990. The document says that we should use utf8mb4 instead of utf8, so we should follow it. --- driver_test.go | 6 +++--- packets.go | 52 +++++++++++++++++++++++++------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/driver_test.go b/driver_test.go index 3ae379b26..f1e4ad71e 100644 --- a/driver_test.go +++ b/driver_test.go @@ -1450,11 +1450,11 @@ func TestCharset(t *testing.T) { mustSetCharset("charset=ascii", "ascii") // when the first charset is invalid, use the second - mustSetCharset("charset=none,utf8", "utf8") + mustSetCharset("charset=none,utf8mb4", "utf8mb4") // when the first charset is valid, use it - mustSetCharset("charset=ascii,utf8", "ascii") - mustSetCharset("charset=utf8,ascii", "utf8") + mustSetCharset("charset=ascii,utf8mb4", "ascii") + mustSetCharset("charset=utf8mb4,ascii", "utf8mb4") } func TestFailingCharset(t *testing.T) { diff --git a/packets.go b/packets.go index 6664e5ae5..1867ecab2 100644 --- a/packets.go +++ b/packets.go @@ -761,40 +761,40 @@ func (rows *textRows) readRow(dest []driver.Value) error { } // RowSet Packet - var n int - var isNull bool - pos := 0 + var ( + n int + isNull bool + pos int = 0 + ) for i := range dest { // Read bytes and convert to string dest[i], isNull, n, err = readLengthEncodedString(data[pos:]) pos += n - if err == nil { - if !isNull { - if !mc.parseTime { - 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 err != nil { + return err + } + + if isNull { + dest[i] = nil + continue + } + + if !mc.parseTime { + continue + } + + // Parse time field + switch rows.rs.columns[i].fieldType { + case fieldTypeTimestamp, + fieldTypeDateTime, + fieldTypeDate, + fieldTypeNewDate: + if dest[i], err = parseDateTime(dest[i].([]byte), mc.cfg.Loc); err != nil { + return err } } - return err // err != nil } return nil