Always defer *sql.Rows.Close and consult with Err (#844)

* Always defer *sql.Rows.Close and consult with Err

database/sql.Rows.Next() makes sure to call Close only after exhausting
result rows which would NOT happen when returning early from a bad Scan.
Close being idempotent makes it a great candidate to get always deferred
regardless of what happens later on the result set.

This change also makes sure call Err() after exhausting Next() and
propagate non-nil results from it as the documentation advises.

Closes #764

Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>

* Override named result parameters in last returns

Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>

* Do the same over new changes that got merged

Signed-off-by: Kiril Vladimiroff <kiril@vladimiroff.org>

Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
This commit is contained in:
Kiril Vladimiroff 2020-02-11 16:12:21 +02:00 committed by GitHub
parent d45f869cdd
commit d5dbe546e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 81 additions and 49 deletions

View file

@ -134,6 +134,7 @@ func (s *outputRoomEventsTopologyStatements) selectEventIDsInRange(
} else if err != nil {
return
}
defer rows.Close() // nolint: errcheck
// Return the IDs.
var eventID string
@ -144,7 +145,7 @@ func (s *outputRoomEventsTopologyStatements) selectEventIDsInRange(
eventIDs = append(eventIDs, eventID)
}
return
return eventIDs, rows.Err()
}
// selectPositionInTopology returns the position of a given event in the
@ -176,6 +177,7 @@ func (s *outputRoomEventsTopologyStatements) selectEventIDsFromPosition(
} else if err != nil {
return
}
defer rows.Close() // nolint: errcheck
// Return the IDs.
var eventID string
for rows.Next() {
@ -184,5 +186,5 @@ func (s *outputRoomEventsTopologyStatements) selectEventIDsFromPosition(
}
eventIDs = append(eventIDs, eventID)
}
return
return eventIDs, rows.Err()
}