Skip to content

Commit d4bc831

Browse files
committed
Unify Bind() result and error handling with v3
A bug was recently fixed with Bind() where it would consume the entire result set when asked to bind to a single object. However the for loop always completed by .Next() returning false. This implicitly closed the rows. Now that we break at the correct time for .One() type queries this bug was introduced. We deferred the close of the rows, so that result set is still opened when eager loading begins. This caused problems with psql transactions when eager loading. Also, awkwardly enough this wasn't a bug in v3 because it was handling things differently, so we simply make the code the same and it should fix it nicely. - Explicitly close rows in queries.Bind() - Fix #318
1 parent 505faa7 commit d4bc831

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/volatiletech/sqlboiler/boilingcore"
1515
)
1616

17-
const sqlBoilerVersion = "2.7.2"
17+
const sqlBoilerVersion = "2.7.3"
1818

1919
var (
2020
flagConfigFile string

queries/reflect.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ func (q *Query) BindP(obj interface{}) {
4646
}
4747

4848
// Bind executes the query and inserts the
49-
// result into the passed in object pointer
49+
// result into the passed in object pointer.
50+
//
51+
// The caller is responsible for closing the rows.
5052
//
5153
// Bind rules:
5254
// - Struct tags control bind, in the form of: `boil:"name,bind"`
@@ -92,7 +94,7 @@ func Bind(rows *sql.Rows, obj interface{}) error {
9294
}
9395

9496
// Bind executes the query and inserts the
95-
// result into the passed in object pointer
97+
// result into the passed in object pointer.
9698
//
9799
// See documentation for boil.Bind()
98100
func (q *Query) Bind(obj interface{}) error {
@@ -105,9 +107,18 @@ func (q *Query) Bind(obj interface{}) error {
105107
if err != nil {
106108
return errors.Wrap(err, "bind failed to execute query")
107109
}
108-
defer rows.Close()
109-
if res := bind(rows, obj, structType, sliceType, bkind); res != nil {
110-
return res
110+
if err = bind(rows, obj, structType, sliceType, bkind); err != nil {
111+
if innerErr := rows.Close(); innerErr != nil {
112+
return errors.Wrapf(err, "error on rows.Close after bind error: %+v", innerErr)
113+
}
114+
115+
return err
116+
}
117+
if err = rows.Close(); err != nil {
118+
return errors.Wrap(err, "failed to clean up rows in bind")
119+
}
120+
if err = rows.Err(); err != nil {
121+
return errors.Wrap(err, "error from rows in bind")
111122
}
112123

113124
if len(q.load) != 0 {

0 commit comments

Comments
 (0)