Skip to content
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

Multi result set, discard after first, for godrv for Stored Procedures #107

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 33 additions & 4 deletions godrv/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"database/sql/driver"
"errors"
"fmt"
"github.com/ziutek/mymysql/mysql"
"github.com/ziutek/mymysql/native"
"io"
"net"
"strconv"
"strings"
"time"

"github.com/ziutek/mymysql/mysql"
"github.com/ziutek/mymysql/native"
)

type conn struct {
Expand Down Expand Up @@ -83,7 +84,7 @@ func (c conn) parseQuery(query string, args []driver.Value) (string, error) {
case float64:
s = strconv.FormatFloat(v, 'e', 12, 64)
default:
panic(fmt.Sprintf("%v (%T) can't be handled by godrv"))
panic(fmt.Sprintf("%v (%T) can't be handled by godrv", a, v))
}
q[n] = query[:i]
q[n+1] = s
Expand Down Expand Up @@ -234,7 +235,9 @@ func (r *rowsRes) Close() error {
if r.my == nil {
return nil // closed before
}
if err := r.my.End(); err != nil {
// Stored Procedure hack for godrv: always ignore multi results
// by using EndAll(r.my) instead of r.my.End()
if err := EndAll(r.my); err != nil {
return errFilter(err)
}
if r.simpleQuery != nil && r.simpleQuery != textQuery {
Expand All @@ -246,6 +249,20 @@ func (r *rowsRes) Close() error {
return nil
}

// Read all unreaded rows and all unread results and discard them.
// Useful for discarding multi result query. See mysql.End
func EndAll(r mysql.Result) error {
_, err := mysql.GetLastRow(r)
if err != nil {
return err
}
nextRes, err := r.NextResult()
if err != nil || nextRes == nil {
return err
}
return EndAll(nextRes)
}

// DATE, DATETIME, TIMESTAMP are treated as they are in Local time zone
func (r *rowsRes) Next(dest []driver.Value) error {
if r.my == nil {
Expand Down Expand Up @@ -273,6 +290,18 @@ func (r *rowsRes) Next(dest []driver.Value) error {
if err != io.EOF {
return errFilter(err)
}

// Stored Procedure hack for godrv: always ignore multi results
nextRes, err := r.my.NextResult()
if err != nil {
return errFilter(err)
}
if nextRes != nil {
if err := EndAll(nextRes); err != nil {
return errFilter(err)
}
}

if r.simpleQuery != nil && r.simpleQuery != textQuery {
if err = r.simpleQuery.Delete(); err != nil {
return errFilter(err)
Expand Down
3 changes: 2 additions & 1 deletion native/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package native
import (
"bufio"
"fmt"
"github.com/ziutek/mymysql/mysql"
"io"
"net"
"reflect"
"strings"
"time"

"github.com/ziutek/mymysql/mysql"
)

type serverInfo struct {
Expand Down