From 72f69004a94c69d0d49452ae8bf9deb3ef675f53 Mon Sep 17 00:00:00 2001 From: Arne Hormann Date: Wed, 9 Jul 2014 14:22:15 +0200 Subject: [PATCH 1/2] Fix handling of queries without columns and rows --- connection.go | 2 ++ rows.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/connection.go b/connection.go index 04607296e..b86b7cdac 100644 --- a/connection.go +++ b/connection.go @@ -225,6 +225,8 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro if resLen > 0 { // Columns rows.columns, err = mc.readColumns(resLen) + } else { + return emptyRows{}, nil } return rows, err } diff --git a/rows.go b/rows.go index df4ef06cb..a7cd27615 100644 --- a/rows.go +++ b/rows.go @@ -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 { @@ -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 +} From 4834ee6ac2096c2a7e06fda17c8d815f20b26905 Mon Sep 17 00:00:00 2001 From: Arne Hormann Date: Thu, 10 Jul 2014 05:18:11 +0200 Subject: [PATCH 2/2] Minor stylistic improvement, added a test --- connection.go | 8 ++++---- driver_test.go | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/connection.go b/connection.go index b86b7cdac..67d3dbee8 100644 --- a/connection.go +++ b/connection.go @@ -222,12 +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) - } else { + if resLen == 0 { + // no columns, no more data return emptyRows{}, nil } + // Columns + rows.columns, err = mc.readColumns(resLen) return rows, err } } diff --git a/driver_test.go b/driver_test.go index ef5b371cf..3e8e86797 100644 --- a/driver_test.go +++ b/driver_test.go @@ -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