简体   繁体   中英

How do I get granular LOAD DATA results from the golang MySQL package?

I am writing a Go program that interacts with a MySQL database. In MySQL, when you do a LOAD DATA query, in addition to the regular X rows affected line you get a line with more granular information:

mysql> LOAD DATA LOCAL INFILE 'many-lines.tsv' REPLACE INTO TABLE test_table (id, timestamp);
Query OK, 6 rows affected (0.01 sec)
Records: 3  Deleted: 3  Skipped: 0  Warnings: 0

As documented here under the section "Statement Result Information".

I would love to be able to access this from my Go program, but I cannot figure out how, or whether it's even possible. sql.DB.Exec() returns a Result , but that only has a RowsAffected field . This contains a sum of rows written + rows deleted and ignores rows skipped, and is therefore ambiguous (write 3, delete 2 and skip 2 is the same as write 5, delete 0 and skip 0).

I looked through the documentation for the Go MySQL driver , but couldn't find anything there that does what I want.

Is there a way to get access to this information?

The information is actually a ER_LOAD_INFO "error" (notionally info) message of the server.

This gets communicated as an informational message in the OK response from the server.

Looking at the decoding of the OK packet in go , it isn't parsing out the info (human readable status information) . When making the connection ensure that clientSessionTrack is part of the connection flags.

So a few small enhancements to the Go MySQL driver and you'll be able to access it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM