2018-12-05 09:35:50 +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-05 09:35:50 +00:00
|
|
|
"storj.io/storj/internal/migrate"
|
2018-12-07 09:59:31 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement"
|
2018-12-05 09:35:50 +00:00
|
|
|
"storj.io/storj/pkg/utils"
|
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
var (
|
|
|
|
// Error is the default satellitedb errs class
|
|
|
|
Error = errs.Class("satellitedb")
|
|
|
|
)
|
|
|
|
|
2018-12-05 09:35:50 +00:00
|
|
|
// DB contains access to different database tables
|
|
|
|
type DB struct {
|
|
|
|
db *dbx.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDB creates instance of database (supports: postgres, sqlite3)
|
|
|
|
func NewDB(databaseURL string) (*DB, error) {
|
|
|
|
dbURL, err := utils.ParseURL(databaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
source := databaseURL
|
|
|
|
if dbURL.Scheme == "sqlite3" {
|
|
|
|
source = dbURL.Path
|
|
|
|
}
|
|
|
|
db, err := dbx.Open(dbURL.Scheme, source)
|
|
|
|
if err != nil {
|
2018-12-07 14:46:42 +00:00
|
|
|
return nil, Error.New("failed opening database %q, %q: %v",
|
|
|
|
dbURL.Scheme, source, err)
|
2018-12-05 09:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &DB{db: db}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 09:59:31 +00:00
|
|
|
// BandwidthAgreement is a getter for bandwidth agreement repository
|
|
|
|
func (db *DB) BandwidthAgreement() bwagreement.DB {
|
|
|
|
return &bandwidthagreement{db: db.db}
|
|
|
|
}
|
|
|
|
|
2018-12-05 09:35:50 +00:00
|
|
|
// // PointerDB is a getter for PointerDB repository
|
|
|
|
// func (db *DB) PointerDB() pointerdb.DB {
|
|
|
|
// return &pointerDB{db: db.db}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // StatDB is a getter for StatDB repository
|
|
|
|
// func (db *DB) StatDB() statdb.DB {
|
|
|
|
// return &statDB{db: db.db}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // OverlayCacheDB is a getter for OverlayCacheDB repository
|
|
|
|
// func (db *DB) OverlayCacheDB() overlay.DB {
|
|
|
|
// return &overlayCacheDB{db: db.db}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // RepairQueueDB is a getter for RepairQueueDB repository
|
|
|
|
// func (db *DB) RepairQueueDB() queue.DB {
|
|
|
|
// return &repairQueueDB{db: db.db}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // AccountingDB is a getter for AccountingDB repository
|
|
|
|
// func (db *DB) AccountingDB() accounting.DB {
|
|
|
|
// return &accountingDB{db: db.db}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// CreateTables is a method for creating all tables for database
|
|
|
|
func (db *DB) CreateTables() error {
|
|
|
|
return migrate.Create("database", db.db)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to close db connection
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
return db.db.Close()
|
|
|
|
}
|