Handled errors from defer rows.Close() and tx.Commit()

This commit is contained in:
Samuel Hawksby-Robinson 2020-12-16 18:17:38 +00:00 committed by Andrea Maria Piana
parent 523d4a7156
commit ba559c5291
2 changed files with 16 additions and 14 deletions

View File

@ -44,14 +44,15 @@ func (db *Database) Close() error {
return db.db.Close() return db.db.Close()
} }
func (db *Database) GetAccounts() ([]Account, error) { func (db *Database) GetAccounts() (rst []Account, err error) {
rows, err := db.db.Query("SELECT a.name, a.loginTimestamp, a.identicon, a.keycardPairing, a.keyUid, ii.name, ii.image_payload, ii.width, ii.height, ii.file_size, ii.resize_target FROM accounts AS a LEFT JOIN identity_images AS ii ON ii.key_uid = a.keyUid ORDER BY loginTimestamp DESC") rows, err := db.db.Query("SELECT a.name, a.loginTimestamp, a.identicon, a.keycardPairing, a.keyUid, ii.name, ii.image_payload, ii.width, ii.height, ii.file_size, ii.resize_target FROM accounts AS a LEFT JOIN identity_images AS ii ON ii.key_uid = a.keyUid ORDER BY loginTimestamp DESC")
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer func() {
err = rows.Close()
}()
var rst []Account
for rows.Next() { for rows.Next() {
acc := Account{} acc := Account{}
accLoginTimestamp := sql.NullInt64{} accLoginTimestamp := sql.NullInt64{}
@ -139,14 +140,15 @@ func (db *Database) DeleteAccount(keyUID string) error {
// Account images // Account images
func (db *Database) GetIdentityImages(keyUID string) ([]*images.IdentityImage, error) { func (db *Database) GetIdentityImages(keyUID string) (iis []*images.IdentityImage, err error) {
rows, err := db.db.Query(`SELECT key_uid, name, image_payload, width, height, file_size, resize_target FROM identity_images WHERE key_uid = ?`, keyUID) rows, err := db.db.Query(`SELECT key_uid, name, image_payload, width, height, file_size, resize_target FROM identity_images WHERE key_uid = ?`, keyUID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer func() {
err = rows.Close()
}()
var iis []*images.IdentityImage
for rows.Next() { for rows.Next() {
ii := &images.IdentityImage{} ii := &images.IdentityImage{}
err = rows.Scan(&ii.KeyUID, &ii.Name, &ii.Payload, &ii.Width, &ii.Height, &ii.FileSize, &ii.ResizeTarget) err = rows.Scan(&ii.KeyUID, &ii.Name, &ii.Payload, &ii.Width, &ii.Height, &ii.FileSize, &ii.ResizeTarget)
@ -171,7 +173,7 @@ func (db *Database) GetIdentityImage(keyUID, it string) (*images.IdentityImage,
return &ii, nil return &ii, nil
} }
func (db *Database) StoreIdentityImages(keyUID string, iis []*images.IdentityImage) error { func (db *Database) StoreIdentityImages(keyUID string, iis []*images.IdentityImage) (err error) {
// Because SQL INSERTs are triggered in a loop use a tx to ensure a single call to the DB. // Because SQL INSERTs are triggered in a loop use a tx to ensure a single call to the DB.
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{}) tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil { if err != nil {

View File

@ -856,26 +856,26 @@ func (db sqlitePersistence) TransactionsToValidate() ([]*TransactionToValidate,
return transactions, nil return transactions, nil
} }
func (db sqlitePersistence) GetWhenChatIdentityLastPublished(chatID string) (*int64, []byte, error) { func (db sqlitePersistence) GetWhenChatIdentityLastPublished(chatID string) (t *int64, hash []byte, err error) {
rows, err := db.db.Query("SELECT clock_value, hash FROM chat_identity_last_published WHERE chat_id = ?", chatID) rows, err := db.db.Query("SELECT clock_value, hash FROM chat_identity_last_published WHERE chat_id = ?", chatID)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
defer rows.Close() defer func() {
err = rows.Close()
}()
var t int64
var hash []byte
for rows.Next() { for rows.Next() {
err = rows.Scan(&t, &hash) err = rows.Scan(t, &hash)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
} }
return &t, hash, nil return t, hash, nil
} }
func (db sqlitePersistence) SaveWhenChatIdentityLastPublished(chatID string, hash []byte) error { func (db sqlitePersistence) SaveWhenChatIdentityLastPublished(chatID string, hash []byte) (err error) {
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{}) tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil { if err != nil {
return err return err