2018-11-08 14:19:42 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
2018-12-07 14:46:42 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2018-12-10 12:29:01 +00:00
|
|
|
"context"
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
"storj.io/storj/internal/migrate"
|
2018-11-08 14:19:42 +00:00
|
|
|
"storj.io/storj/pkg/satellite"
|
|
|
|
"storj.io/storj/pkg/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
var (
|
|
|
|
// Error is the default satellitedb errs class
|
|
|
|
Error = errs.Class("satellitedb")
|
|
|
|
)
|
|
|
|
|
2018-11-08 14:19:42 +00:00
|
|
|
// Database contains access to different satellite databases
|
|
|
|
type Database struct {
|
|
|
|
db *dbx.DB
|
2018-12-10 12:29:01 +00:00
|
|
|
tx *dbx.Tx
|
|
|
|
|
|
|
|
methods dbx.Methods
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New - constructor for DB
|
2018-12-10 12:29:01 +00:00
|
|
|
func New(driver, source string) (*Database, error) {
|
2018-11-08 14:19:42 +00:00
|
|
|
db, err := dbx.Open(driver, source)
|
|
|
|
if err != nil {
|
2018-12-07 14:46:42 +00:00
|
|
|
return nil, Error.New("failed opening database %q, %q: %v",
|
|
|
|
driver, source, err)
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
database := &Database{
|
2018-12-10 12:29:01 +00:00
|
|
|
db: db,
|
|
|
|
methods: db,
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return database, nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// Users is getter a for Users repository
|
2018-11-08 14:19:42 +00:00
|
|
|
func (db *Database) Users() satellite.Users {
|
2018-12-10 12:29:01 +00:00
|
|
|
return &users{db.methods}
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// Projects is a getter for Projects repository
|
2018-11-13 08:27:42 +00:00
|
|
|
func (db *Database) Projects() satellite.Projects {
|
2018-12-10 12:29:01 +00:00
|
|
|
return &projects{db.methods}
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// ProjectMembers is a getter for ProjectMembers repository
|
|
|
|
func (db *Database) ProjectMembers() satellite.ProjectMembers {
|
2018-12-10 12:29:01 +00:00
|
|
|
return &projectMembers{db.methods}
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 14:00:53 +00:00
|
|
|
// APIKeys is a getter for APIKeys repository
|
|
|
|
func (db *Database) APIKeys() satellite.APIKeys {
|
|
|
|
return &apikeys{db.methods}
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:19:42 +00:00
|
|
|
// CreateTables is a method for creating all tables for satellitedb
|
|
|
|
func (db *Database) CreateTables() error {
|
2018-12-10 12:29:01 +00:00
|
|
|
if db.db == nil {
|
|
|
|
return errs.New("Connection is closed")
|
|
|
|
}
|
2018-11-14 12:45:49 +00:00
|
|
|
return migrate.Create("satellitedb", db.db)
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to close db connection
|
|
|
|
func (db *Database) Close() error {
|
2018-12-10 12:29:01 +00:00
|
|
|
if db.db == nil {
|
|
|
|
return errs.New("Connection is closed")
|
|
|
|
}
|
2018-11-08 14:19:42 +00:00
|
|
|
return db.db.Close()
|
|
|
|
}
|
2018-12-10 12:29:01 +00:00
|
|
|
|
|
|
|
// BeginTx is a method for opening transaction
|
|
|
|
func (db *Database) BeginTx(ctx context.Context) (satellite.DBTx, error) {
|
|
|
|
if db.db == nil {
|
|
|
|
return nil, errs.New("DB is not initialized!")
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := db.db.Open(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DBTx{
|
|
|
|
Database: &Database{
|
|
|
|
tx: tx,
|
|
|
|
methods: tx,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DBTx extends Database with transaction scope
|
|
|
|
type DBTx struct {
|
|
|
|
*Database
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit is a method for committing and closing transaction
|
|
|
|
func (db *DBTx) Commit() error {
|
|
|
|
if db.tx == nil {
|
|
|
|
return errs.New("begin transaction before commit it!")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback is a method for rollback and closing transaction
|
|
|
|
func (db *DBTx) Rollback() error {
|
|
|
|
if db.tx == nil {
|
|
|
|
return errs.New("begin transaction before rollback it!")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.tx.Rollback()
|
|
|
|
}
|