Properly close db cursors after use

This commit is contained in:
Mike Schwörer 2022-12-08 11:31:52 +01:00
parent 7db70e392b
commit dd2f3baa0c
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 40 additions and 0 deletions

View File

@ -26,6 +26,11 @@ func (db *Database) ReadSchema(ctx context.Context) (retval int, reterr error) {
return 0, nil return 0, nil
} }
err = r1.Close()
if err != nil {
return 0, err
}
r2, err := db.db.Query(ctx, "SELECT value_int FROM meta WHERE meta_key = :key", sq.PP{"key": "schema"}) r2, err := db.db.Query(ctx, "SELECT value_int FROM meta WHERE meta_key = :key", sq.PP{"key": "schema"})
if err != nil { if err != nil {
return 0, err return 0, err
@ -49,6 +54,11 @@ func (db *Database) ReadSchema(ctx context.Context) (retval int, reterr error) {
return 0, err return 0, err
} }
err = r2.Close()
if err != nil {
return 0, err
}
return dbschema, nil return dbschema, nil
} }
@ -119,6 +129,11 @@ func (db *Database) ReadMetaString(ctx context.Context, key string) (retval *str
return nil, err return nil, err
} }
err = r2.Close()
if err != nil {
return nil, err
}
return langext.Ptr(value), nil return langext.Ptr(value), nil
} }
@ -146,6 +161,11 @@ func (db *Database) ReadMetaInt(ctx context.Context, key string) (retval *int64,
return nil, err return nil, err
} }
err = r2.Close()
if err != nil {
return nil, err
}
return langext.Ptr(value), nil return langext.Ptr(value), nil
} }
@ -173,6 +193,11 @@ func (db *Database) ReadMetaReal(ctx context.Context, key string) (retval *float
return nil, err return nil, err
} }
err = r2.Close()
if err != nil {
return nil, err
}
return langext.Ptr(value), nil return langext.Ptr(value), nil
} }
@ -200,6 +225,11 @@ func (db *Database) ReadMetaBlob(ctx context.Context, key string) (retval *[]byt
return nil, err return nil, err
} }
err = r2.Close()
if err != nil {
return nil, err
}
return langext.Ptr(value), nil return langext.Ptr(value), nil
} }

View File

@ -31,10 +31,16 @@ func scanSingle[TData any](rows *sqlx.Rows) (TData, error) {
return *new(TData), err return *new(TData), err
} }
if rows.Next() { if rows.Next() {
_ = rows.Close()
return *new(TData), errors.New("sql returned more than onw row") return *new(TData), errors.New("sql returned more than onw row")
} }
err = rows.Close()
if err != nil {
return *new(TData), err
}
return data, nil return data, nil
} else { } else {
_ = rows.Close()
return *new(TData), sql.ErrNoRows return *new(TData), sql.ErrNoRows
} }
} }
@ -49,5 +55,9 @@ func scanAll[TData any](rows *sqlx.Rows) ([]TData, error) {
} }
res = append(res, data) res = append(res, data)
} }
err := rows.Close()
if err != nil {
return nil, err
}
return res, nil return res, nil
} }