Skip to content

Commit 57fe6e4

Browse files
committed
Fix Row-Fetching
1 parent 7af5412 commit 57fe6e4

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro
256256
return nil, e
257257
}
258258

259-
rows := mysqlRows{&rowsContent{mc, false, resLen, nil}}
259+
rows := mysqlRows{&rowsContent{mc, false, nil, false}}
260260

261261
if resLen > 0 {
262262
// Columns

rows.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ type mysqlField struct {
2424
type rowsContent struct {
2525
mc *mysqlConn
2626
binary bool
27-
unread int
2827
columns []mysqlField
28+
eof bool
2929
}
3030

3131
type mysqlRows struct {
@@ -47,7 +47,7 @@ func (rows mysqlRows) Close() (e error) {
4747
}()
4848

4949
// Remove unread packets from stream
50-
if rows.content.unread > -1 {
50+
if !rows.content.eof {
5151
if rows.content.mc == nil {
5252
return errors.New("Invalid Connection")
5353
}
@@ -66,36 +66,38 @@ func (rows mysqlRows) Close() (e error) {
6666
// when the dest type is know, which makes type conversion easier and avoids
6767
// unnecessary conversions.
6868
func (rows mysqlRows) Next(dest []driver.Value) error {
69-
if rows.content.unread > 0 {
70-
if rows.content.mc == nil {
71-
return errors.New("Invalid Connection")
72-
}
69+
if rows.content.eof {
70+
return io.EOF
71+
}
7372

74-
columnsCount := cap(dest)
73+
if rows.content.mc == nil {
74+
return errors.New("Invalid Connection")
75+
}
7576

76-
// Fetch next row from stream
77-
var row *[]*[]byte
78-
var e error
79-
if rows.content.binary {
80-
row, e = rows.content.mc.readBinaryRow(rows.content)
81-
} else {
82-
row, e = rows.content.mc.readRow(columnsCount)
83-
}
84-
rows.content.unread--
77+
columnsCount := cap(dest)
8578

86-
if e != nil {
87-
return e
79+
// Fetch next row from stream
80+
var row *[]*[]byte
81+
var e error
82+
if rows.content.binary {
83+
row, e = rows.content.mc.readBinaryRow(rows.content)
84+
} else {
85+
row, e = rows.content.mc.readRow(columnsCount)
86+
}
87+
88+
if e != nil {
89+
if e == io.EOF {
90+
rows.content.eof = true
8891
}
92+
return e
93+
}
8994

90-
for i := 0; i < columnsCount; i++ {
91-
if (*row)[i] == nil {
92-
dest[i] = nil
93-
} else {
94-
dest[i] = *(*row)[i]
95-
}
95+
for i := 0; i < columnsCount; i++ {
96+
if (*row)[i] == nil {
97+
dest[i] = nil
98+
} else {
99+
dest[i] = *(*row)[i]
96100
}
97-
} else {
98-
return io.EOF
99101
}
100102

101103
return nil

statement.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (stmt mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
100100
return nil, e
101101
}
102102

103-
rows := mysqlRows{&rowsContent{stmt.mc, true, resLen, nil}}
103+
rows := mysqlRows{&rowsContent{stmt.mc, true, nil, false}}
104104

105105
if resLen > 0 {
106106
// Columns

0 commit comments

Comments
 (0)