Commit f7e223db authored by gwenn's avatar gwenn

Implement RowsNextResultSet

parent 7a9d5708
...@@ -316,7 +316,37 @@ func (r *rowsImpl) Close() error { ...@@ -316,7 +316,37 @@ func (r *rowsImpl) Close() error {
return r.s.s.Reset() return r.s.s.Reset()
} }
// TODO HasNextResultSet && Stmt.tail ? func (r *rowsImpl) HasNextResultSet() bool {
return len(r.s.s.tail) > 0
}
func (r *rowsImpl) NextResultSet() error {
currentStmt := r.s.s
nextQuery := currentStmt.tail
var nextStmt *Stmt
var err error
for len(nextQuery) > 0 {
nextStmt, err = currentStmt.c.Prepare(nextQuery)
if err != nil {
return err
} else if nextStmt.stmt == nil {
// this happens for a comment or white-space
nextQuery = nextStmt.tail
continue
}
break
}
if nextStmt == nil {
return io.EOF
}
// TODO close vs reset ?
err = currentStmt.Finalize()
if err != nil {
return err
}
r.s.s = nextStmt
return nil
}
func (r *rowsImpl) ColumnTypeScanType(index int) reflect.Type { func (r *rowsImpl) ColumnTypeScanType(index int) reflect.Type {
switch r.s.s.ColumnType(index) { switch r.s.s.ColumnType(index) {
......
...@@ -514,3 +514,66 @@ func TestNilAndEmptyBytes(t *testing.T) { ...@@ -514,3 +514,66 @@ func TestNilAndEmptyBytes(t *testing.T) {
checkSqlRowsClose(rows, t) checkSqlRowsClose(rows, t)
} }
} }
func TestMultipleResultSets(t *testing.T) {
db := sqlOpen(t)
defer checkSqlDbClose(db, t)
rows, err := db.Query("SELECT 1; SELECT 2; SELECT 3")
if err != nil {
t.Fatal(err)
}
defer rows.Close()
var i int
for rows.Next() {
if err := rows.Scan(&i); err != nil {
t.Fatal(err)
}
if i != 1 {
t.Fatalf("expected 1, got %d", i)
}
}
if !rows.NextResultSet() {
t.Fatal("expected more result sets", rows.Err())
}
for rows.Next() {
if err := rows.Scan(&i); err != nil {
t.Fatal(err)
}
if i != 2 {
t.Fatalf("expected 2, got %d", i)
}
}
// Make sure that if we ignore a result we can still query.
rows, err = db.Query("select 4; select 5")
if err != nil {
t.Fatal(err)
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(&i); err != nil {
t.Fatal(err)
}
if i != 4 {
t.Fatalf("expected 4, got %d", i)
}
}
if !rows.NextResultSet() {
t.Fatal("expected more result sets", rows.Err())
}
for rows.Next() {
if err := rows.Scan(&i); err != nil {
t.Fatal(err)
}
if i != 5 {
t.Fatalf("expected 5, got %d", i)
}
}
if rows.NextResultSet() {
t.Fatal("unexpected result set")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment