Skip to content

Fix handling of queries without columns and rows #255

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 2 commits into from
Aug 13, 2014
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
8 changes: 5 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,12 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro
rows := new(textRows)
rows.mc = mc

if resLen > 0 {
// Columns
rows.columns, err = mc.readColumns(resLen)
if resLen == 0 {
// no columns, no more data
return emptyRows{}, nil
}
// Columns
rows.columns, err = mc.readColumns(resLen)
return rows, err
}
}
Expand Down
11 changes: 11 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ func (dbt *DBTest) mustQuery(query string, args ...interface{}) (rows *sql.Rows)
return rows
}

func TestEmptyQuery(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
// just a comment, no query
rows := dbt.mustQuery("--")
// will hang before #255
if rows.Next() {
dbt.Errorf("Next on rows must be false")
}
})
}

func TestCRUD(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
// Create Table
Expand Down
14 changes: 14 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type textRows struct {
mysqlRows
}

type emptyRows struct{}

func (rows *mysqlRows) Columns() []string {
columns := make([]string, len(rows.columns))
for i := range columns {
Expand Down Expand Up @@ -84,3 +86,15 @@ func (rows *textRows) Next(dest []driver.Value) error {
}
return io.EOF
}

func (rows emptyRows) Columns() []string {
return nil
}

func (rows emptyRows) Close() error {
return nil
}

func (rows emptyRows) Next(dest []driver.Value) error {
return io.EOF
}