fix: close resultsets so we don't leak them (#1809)
* fix: close resultsets so we don't leak them * Refactor browsers/database To implement PR suggestions and improve code quality. * Refactor services/permissions/database To implement PR suggestions and improve code quality. Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
This commit is contained in:
parent
77d9d0f4c9
commit
be683556ff
|
@ -353,6 +353,7 @@ func (db *Database) GetAccounts() ([]Account, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
accounts := []Account{}
|
||||
pubkey := []byte{}
|
||||
for rows.Next() {
|
||||
|
@ -456,6 +457,7 @@ func (db *Database) GetAddresses() (rst []types.Address, err error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
addr := types.Address{}
|
||||
err = rows.Scan(&addr)
|
||||
|
|
|
@ -42,6 +42,7 @@ func (db *Database) GetAccounts() ([]Account, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
rst := []Account{}
|
||||
inthelper := sql.NullInt64{}
|
||||
for rows.Next() {
|
||||
|
|
|
@ -26,6 +26,7 @@ func (s *sqlitePersistence) GetActiveInstallations(maxInstallations int, identit
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var (
|
||||
|
|
|
@ -77,6 +77,7 @@ func (s *sqlitePersistence) Get(identity []byte, installationIDs []string) (*Res
|
|||
if err != nil && err != sql.ErrNoRows {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var installationID string
|
||||
|
@ -102,6 +103,7 @@ func (s *sqlitePersistence) All() ([][][]byte, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var secret []byte
|
||||
|
|
|
@ -39,6 +39,7 @@ func (s *sqlitePersistence) All() (map[string][]byte, error) {
|
|||
if err != nil && err != sql.ErrNoRows {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var (
|
||||
|
|
|
@ -39,6 +39,7 @@ func (s *sqlitePersistence) All() (map[string][]byte, error) {
|
|||
if err != nil && err != sql.ErrNoRows {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var (
|
||||
|
|
|
@ -28,11 +28,7 @@ type Browser struct {
|
|||
}
|
||||
|
||||
func (db *Database) InsertBrowser(browser Browser) (err error) {
|
||||
var (
|
||||
tx *sql.Tx
|
||||
insert *sql.Stmt
|
||||
)
|
||||
tx, err = db.db.Begin()
|
||||
tx, err := db.db.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -43,38 +39,37 @@ func (db *Database) InsertBrowser(browser Browser) (err error) {
|
|||
}
|
||||
_ = tx.Rollback()
|
||||
}()
|
||||
insert, err = tx.Prepare("INSERT OR REPLACE INTO browsers(id, name, timestamp, dapp, historyIndex) VALUES(?, ?, ?, ?, ?)")
|
||||
|
||||
bInsert, err := tx.Prepare("INSERT OR REPLACE INTO browsers(id, name, timestamp, dapp, historyIndex) VALUES(?, ?, ?, ?, ?)")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = insert.Exec(browser.ID, browser.Name, browser.Timestamp, browser.Dapp, browser.HistoryIndex)
|
||||
insert.Close()
|
||||
_, err = bInsert.Exec(browser.ID, browser.Name, browser.Timestamp, browser.Dapp, browser.HistoryIndex)
|
||||
bInsert.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(browser.History) == 0 {
|
||||
return
|
||||
}
|
||||
insert, err = tx.Prepare("INSERT INTO browsers_history(browser_id, history) VALUES(?, ?)")
|
||||
bhInsert, err := tx.Prepare("INSERT INTO browsers_history(browser_id, history) VALUES(?, ?)")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer insert.Close()
|
||||
defer bhInsert.Close()
|
||||
for _, history := range browser.History {
|
||||
_, err = insert.Exec(browser.ID, history)
|
||||
_, err = bhInsert.Exec(browser.ID, history)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (db *Database) GetBrowsers() (rst []*Browser, err error) {
|
||||
var (
|
||||
tx *sql.Tx
|
||||
rows *sql.Rows
|
||||
)
|
||||
tx, err = db.db.Begin()
|
||||
tx, err := db.db.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -85,36 +80,41 @@ func (db *Database) GetBrowsers() (rst []*Browser, err error) {
|
|||
}
|
||||
_ = tx.Rollback()
|
||||
}()
|
||||
|
||||
// FULL and RIGHT joins are not supported
|
||||
rows, err = tx.Query("SELECT id, name, timestamp, dapp, historyIndex FROM browsers ORDER BY timestamp DESC")
|
||||
bRows, err := tx.Query("SELECT id, name, timestamp, dapp, historyIndex FROM browsers ORDER BY timestamp DESC")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer bRows.Close()
|
||||
browsers := map[string]*Browser{}
|
||||
for rows.Next() {
|
||||
for bRows.Next() {
|
||||
browser := Browser{}
|
||||
err = rows.Scan(&browser.ID, &browser.Name, &browser.Timestamp, &browser.Dapp, &browser.HistoryIndex)
|
||||
err = bRows.Scan(&browser.ID, &browser.Name, &browser.Timestamp, &browser.Dapp, &browser.HistoryIndex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
browsers[browser.ID] = &browser
|
||||
rst = append(rst, &browser)
|
||||
}
|
||||
rows, err = tx.Query("SELECT browser_id, history from browsers_history")
|
||||
|
||||
bhRows, err := tx.Query("SELECT browser_id, history from browsers_history")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer bhRows.Close()
|
||||
var (
|
||||
id string
|
||||
history string
|
||||
)
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&id, &history)
|
||||
for bhRows.Next() {
|
||||
err = bhRows.Scan(&id, &history)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
browsers[id].History = append(browsers[id].History, history)
|
||||
}
|
||||
|
||||
return rst, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -234,6 +234,7 @@ func (d *Database) Topics() ([]MailserverTopic, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var (
|
||||
|
@ -281,6 +282,7 @@ func (d *Database) ChatRequestRanges() ([]ChatRequestRange, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var req ChatRequestRange
|
||||
|
|
|
@ -24,11 +24,7 @@ type DappPermissions struct {
|
|||
}
|
||||
|
||||
func (db *Database) AddPermissions(perms DappPermissions) (err error) {
|
||||
var (
|
||||
tx *sql.Tx
|
||||
insert *sql.Stmt
|
||||
)
|
||||
tx, err = db.db.Begin()
|
||||
tx, err := db.db.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -39,25 +35,27 @@ func (db *Database) AddPermissions(perms DappPermissions) (err error) {
|
|||
}
|
||||
_ = tx.Rollback()
|
||||
}()
|
||||
insert, err = tx.Prepare("INSERT OR REPLACE INTO dapps(name) VALUES(?)")
|
||||
|
||||
dInsert, err := tx.Prepare("INSERT OR REPLACE INTO dapps(name) VALUES(?)")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = insert.Exec(perms.Name)
|
||||
insert.Close()
|
||||
_, err = dInsert.Exec(perms.Name)
|
||||
dInsert.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(perms.Permissions) == 0 {
|
||||
return
|
||||
}
|
||||
insert, err = tx.Prepare("INSERT INTO permissions(dapp_name, permission) VALUES(?, ?)")
|
||||
pInsert, err := tx.Prepare("INSERT INTO permissions(dapp_name, permission) VALUES(?, ?)")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer insert.Close()
|
||||
defer pInsert.Close()
|
||||
for _, perm := range perms.Permissions {
|
||||
_, err = insert.Exec(perms.Name, perm)
|
||||
_, err = pInsert.Exec(perms.Name, perm)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -66,11 +64,7 @@ func (db *Database) AddPermissions(perms DappPermissions) (err error) {
|
|||
}
|
||||
|
||||
func (db *Database) GetPermissions() (rst []DappPermissions, err error) {
|
||||
var (
|
||||
tx *sql.Tx
|
||||
rows *sql.Rows
|
||||
)
|
||||
tx, err = db.db.Begin()
|
||||
tx, err := db.db.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -81,30 +75,34 @@ func (db *Database) GetPermissions() (rst []DappPermissions, err error) {
|
|||
}
|
||||
_ = tx.Rollback()
|
||||
}()
|
||||
|
||||
// FULL and RIGHT joins are not supported
|
||||
rows, err = tx.Query("SELECT name FROM dapps")
|
||||
dRows, err := tx.Query("SELECT name FROM dapps")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer dRows.Close()
|
||||
dapps := map[string]*DappPermissions{}
|
||||
for rows.Next() {
|
||||
for dRows.Next() {
|
||||
perms := DappPermissions{}
|
||||
err = rows.Scan(&perms.Name)
|
||||
err = dRows.Scan(&perms.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dapps[perms.Name] = &perms
|
||||
}
|
||||
rows, err = tx.Query("SELECT dapp_name, permission from permissions")
|
||||
|
||||
pRows, err := tx.Query("SELECT dapp_name, permission from permissions")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer pRows.Close()
|
||||
var (
|
||||
name string
|
||||
permission string
|
||||
)
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&name, &permission)
|
||||
for pRows.Next() {
|
||||
err = pRows.Scan(&name, &permission)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -114,6 +112,7 @@ func (db *Database) GetPermissions() (rst []DappPermissions, err error) {
|
|||
for key := range dapps {
|
||||
rst = append(rst, *dapps[key])
|
||||
}
|
||||
|
||||
return rst, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue