stasatdaglabs d14809694f
[NOD-1223] Reorganize directory structure (#874)
* [NOD-1223] Delete unused files/packages.

* [NOD-1223] Move signal and limits to the os package.

* [NOD-1223] Put database and dbaccess into the db package.

* [NOD-1223] Fold the logs package into the logger package.

* [NOD-1223] Rename domainmessage to appmessage.

* [NOD-1223] Rename to/from DomainMessage to AppMessage.

* [NOD-1223] Move appmessage to the app packge.

* [NOD-1223] Move protocol to the app packge.

* [NOD-1223] Move the network package to the infrastructure packge.

* [NOD-1223] Rename cmd to executables.

* [NOD-1223] Fix go.doc in the logger package.
2020-08-18 10:26:39 +03:00

55 lines
1.5 KiB
Go

package dbaccess
import (
"github.com/kaspanet/kaspad/infrastructure/db/database"
)
// Context is an interface type representing the context in which queries run, currently relating to the
// existence or non-existence of a database transaction
// Call `.NoTx()` or `.NewTx()` to acquire a Context
type Context interface {
accessor() (database.DataAccessor, error)
}
type noTxContext struct {
backend *DatabaseContext
}
func (ctx *noTxContext) accessor() (database.DataAccessor, error) {
return ctx.backend.db, nil
}
// TxContext represents a database context with an attached database transaction
type TxContext struct {
dbTransaction database.Transaction
}
// NewTx returns an instance of TxContext with a new database transaction
func (ctx *DatabaseContext) NewTx() (*TxContext, error) {
dbTransaction, err := ctx.db.Begin()
if err != nil {
return nil, err
}
return &TxContext{dbTransaction: dbTransaction}, nil
}
func (ctx *TxContext) accessor() (database.DataAccessor, error) {
return ctx.dbTransaction, nil
}
// Commit commits the transaction attached to this TxContext
func (ctx *TxContext) Commit() error {
return ctx.dbTransaction.Commit()
}
// Rollback rolls-back the transaction attached to this TxContext
func (ctx *TxContext) Rollback() error {
return ctx.dbTransaction.Rollback()
}
// RollbackUnlessClosed rolls-back the transaction attached to this TxContext,
// unless the transaction had already been closed using either Rollback or Commit.
func (ctx *TxContext) RollbackUnlessClosed() error {
return ctx.dbTransaction.RollbackUnlessClosed()
}