Skip to content

Commit 3361273

Browse files
infile: fix reading from empty file (go-sql-driver#590)
1 parent e5cc7f3 commit 3361273

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

driver_test.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -1054,22 +1054,36 @@ func TestLoadData(t *testing.T) {
10541054
dbt.Fatalf("rows count mismatch. Got %d, want 4", i)
10551055
}
10561056
}
1057+
1058+
dbt.db.Exec("DROP TABLE IF EXISTS test")
1059+
dbt.mustExec("CREATE TABLE test (id INT NOT NULL PRIMARY KEY, value TEXT NOT NULL) CHARACTER SET utf8")
1060+
1061+
// Local File
10571062
file, err := ioutil.TempFile("", "gotest")
10581063
defer os.Remove(file.Name())
10591064
if err != nil {
10601065
dbt.Fatal(err)
10611066
}
1062-
file.WriteString("1\ta string\n2\ta string containing a \\t\n3\ta string containing a \\n\n4\ta string containing both \\t\\n\n")
1063-
file.Close()
1067+
RegisterLocalFile(file.Name())
10641068

1065-
dbt.db.Exec("DROP TABLE IF EXISTS test")
1066-
dbt.mustExec("CREATE TABLE test (id INT NOT NULL PRIMARY KEY, value TEXT NOT NULL) CHARACTER SET utf8")
1069+
// Try first with empty file
1070+
dbt.mustExec(fmt.Sprintf("LOAD DATA LOCAL INFILE %q INTO TABLE test", file.Name()))
1071+
var count int
1072+
err = dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&count)
1073+
if err != nil {
1074+
dbt.Fatal(err.Error())
1075+
}
1076+
if count != 0 {
1077+
dbt.Fatalf("unexpected row count: got %d, want 0", count)
1078+
}
10671079

1068-
// Local File
1069-
RegisterLocalFile(file.Name())
1080+
// Then fille File with data and try to load it
1081+
file.WriteString("1\ta string\n2\ta string containing a \\t\n3\ta string containing a \\n\n4\ta string containing both \\t\\n\n")
1082+
file.Close()
10701083
dbt.mustExec(fmt.Sprintf("LOAD DATA LOCAL INFILE %q INTO TABLE test", file.Name()))
10711084
verifyLoadDataResult()
1072-
// negative test
1085+
1086+
// Try with non-existing file
10731087
_, err = dbt.db.Exec("LOAD DATA LOCAL INFILE 'doesnotexist' INTO TABLE test")
10741088
if err == nil {
10751089
dbt.Fatal("load non-existent file didn't fail")

infile.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
147147
}
148148

149149
// send content packets
150-
if err == nil {
150+
// if packetSize == 0, the Reader contains no data
151+
if err == nil && packetSize > 0 {
151152
data := make([]byte, 4+packetSize)
152153
var n int
153154
for err == nil {

0 commit comments

Comments
 (0)