Skip to content

Make BinaryRows and TextRows distinct types #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro
var resLen int
resLen, err = mc.readResultSetHeaderPacket()
if err == nil {
rows := &mysqlRows{mc, nil, false}
rows := new(textRows)
rows.mc = mc

if resLen > 0 {
// Columns
Expand All @@ -238,7 +239,8 @@ func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
// Read Result
resLen, err := mc.readResultSetHeaderPacket()
if err == nil {
rows := &mysqlRows{mc, nil, false}
rows := new(textRows)
rows.mc = mc

if resLen > 0 {
// Columns
Expand Down
4 changes: 2 additions & 2 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func (mc *mysqlConn) readColumns(count int) ([]mysqlField, error) {

// Read Packets as Field Packets until EOF-Packet or an Error appears
// http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::ResultsetRow
func (rows *mysqlRows) readRow(dest []driver.Value) error {
func (rows *textRows) readRow(dest []driver.Value) error {
mc := rows.mc

data, err := mc.readPacket()
Expand Down Expand Up @@ -993,7 +993,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
}

// http://dev.mysql.com/doc/internals/en/binary-protocol-resultset-row.html
func (rows *mysqlRows) readBinaryRow(dest []driver.Value) error {
func (rows *binaryRows) readRow(dest []driver.Value) error {
data, err := rows.mc.readPacket()
if err != nil {
return err
Expand Down
50 changes: 34 additions & 16 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ type mysqlField struct {
type mysqlRows struct {
mc *mysqlConn
columns []mysqlField
binary bool
}

type binaryRows struct {
mysqlRows
}

type textRows struct {
mysqlRows
}

func (rows *mysqlRows) Columns() []string {
Expand All @@ -41,28 +48,39 @@ func (rows *mysqlRows) Close() error {
if mc.netConn == nil {
return errInvalidConn
}

// Remove unread packets from stream
err := mc.readUntilEOF()
rows.mc = nil
return err
}

func (rows *mysqlRows) Next(dest []driver.Value) (err error) {
mc := rows.mc
if mc == nil {
return io.EOF
}
if mc.netConn == nil {
return errInvalidConn
}
// Fetch next row from stream
if rows.binary {
err = rows.readBinaryRow(dest)
} else {
err = rows.readRow(dest)
func (rows *binaryRows) Next(dest []driver.Value) error {
if mc := rows.mc; mc != nil {
if mc.netConn == nil {
return errInvalidConn
}

// Fetch next row from stream
if err := rows.readRow(dest); err != io.EOF {
return err
}
rows.mc = nil
}
if err == io.EOF {
return io.EOF
}

func (rows *textRows) Next(dest []driver.Value) error {
if mc := rows.mc; mc != nil {
if mc.netConn == nil {
return errInvalidConn
}

// Fetch next row from stream
if err := rows.readRow(dest); err != io.EOF {
return err
}
rows.mc = nil
}
return
return io.EOF
}
3 changes: 2 additions & 1 deletion statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
return nil, err
}

rows := &mysqlRows{mc, nil, true}
rows := new(binaryRows)
rows.mc = mc

if resLen > 0 {
// Columns
Expand Down