Svarog 6b5b4bfb2a
[NOD-1164] Remove the singleton from dbaccess, to enable multiple db connections in same run (#806)
* [NOD-1164] Defined DatabaseContext as the basic object of dbaccess

* [NOD-1164] Update everything to use databaseContext

* [NOD-1164] Fix tests

* [NOD-1164] Add comments

* [NOD-1164] Removed databaseContext from blockNode

* [NOD-1164] Enforce DatabaseContext != nil

* [NOD-1164] Remove redundant and wrong comment line
2020-07-21 12:02:44 +03:00

31 lines
733 B
Go

package dbaccess
import (
"github.com/kaspanet/kaspad/database"
"github.com/kaspanet/kaspad/database/ffldb"
)
// DatabaseContext represents a context in which all database queries run
type DatabaseContext struct {
db database.Database
*noTxContext
}
// New creates a new DatabaseContext with database is in the specified `path`
func New(path string) (*DatabaseContext, error) {
db, err := ffldb.Open(path)
if err != nil {
return nil, err
}
databaseContext := &DatabaseContext{db: db}
databaseContext.noTxContext = &noTxContext{backend: databaseContext}
return databaseContext, nil
}
// Close closes the DatabaseContext's connection, if it's open
func (ctx *DatabaseContext) Close() error {
return ctx.db.Close()
}