Skip to content

Commit 105a8fd

Browse files
committed
Merge pull request go-sql-driver#255 from arnehormann/result-without-columns
Fix handling of queries without columns and rows
2 parents 7094cf0 + 4834ee6 commit 105a8fd

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

connection.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,12 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro
222222
rows := new(textRows)
223223
rows.mc = mc
224224

225-
if resLen > 0 {
226-
// Columns
227-
rows.columns, err = mc.readColumns(resLen)
225+
if resLen == 0 {
226+
// no columns, no more data
227+
return emptyRows{}, nil
228228
}
229+
// Columns
230+
rows.columns, err = mc.readColumns(resLen)
229231
return rows, err
230232
}
231233
}

driver_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ func (dbt *DBTest) mustQuery(query string, args ...interface{}) (rows *sql.Rows)
117117
return rows
118118
}
119119

120+
func TestEmptyQuery(t *testing.T) {
121+
runTests(t, dsn, func(dbt *DBTest) {
122+
// just a comment, no query
123+
rows := dbt.mustQuery("--")
124+
// will hang before #255
125+
if rows.Next() {
126+
dbt.Errorf("Next on rows must be false")
127+
}
128+
})
129+
}
130+
120131
func TestCRUD(t *testing.T) {
121132
runTests(t, dsn, func(dbt *DBTest) {
122133
// Create Table

rows.go

+14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type textRows struct {
3232
mysqlRows
3333
}
3434

35+
type emptyRows struct{}
36+
3537
func (rows *mysqlRows) Columns() []string {
3638
columns := make([]string, len(rows.columns))
3739
for i := range columns {
@@ -84,3 +86,15 @@ func (rows *textRows) Next(dest []driver.Value) error {
8486
}
8587
return io.EOF
8688
}
89+
90+
func (rows emptyRows) Columns() []string {
91+
return nil
92+
}
93+
94+
func (rows emptyRows) Close() error {
95+
return nil
96+
}
97+
98+
func (rows emptyRows) Next(dest []driver.Value) error {
99+
return io.EOF
100+
}

0 commit comments

Comments
 (0)