db

package
v1.5.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2025 License: Apache-2.0 Imports: 8 Imported by: 154

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// APICounterOpts define the counter opts for database APIs
	APICounterOpts = metrics.CounterOpts{
		Namespace:    "db_api_request",
		Subsystem:    "",
		Name:         "count",
		Help:         "Number of requests made to a database API",
		LabelNames:   []string{"ca_name", "func_name", "dbapi_name"},
		StatsdFormat: "%{#fqname}.%{ca_name}.%{func_name}.%{dbapi_name}",
	}

	// APIDurationOpts define the duration opts for database APIs
	APIDurationOpts = metrics.HistogramOpts{
		Namespace:    "db_api_request",
		Subsystem:    "",
		Name:         "duration",
		Help:         "Time taken in seconds for the request to a database API to be completed",
		LabelNames:   []string{"ca_name", "func_name", "dbapi_name"},
		StatsdFormat: "%{#fqname}.%{ca_name}.%{func_name}.%{dbapi_name}",
	}
)

Functions

func CurrentDBLevels

func CurrentDBLevels(db FabricCADB) (*util.Levels, error)

CurrentDBLevels returns current levels from the database

func Migrate

func Migrate(migrator Migrator, currentLevels, srvLevels *util.Levels) error

Migrate updates the database tables to use the latest schema and does data migration if needed

Types

type AffiliationRecord

type AffiliationRecord struct {
	ID     int    `db:"id"`
	Name   string `db:"name"`
	Prekey string `db:"prekey"`
	Level  int    `db:"level"`
}

AffiliationRecord defines the properties of an affiliation

type CertRecord

type CertRecord struct {
	ID    string `db:"id"`
	Level int    `db:"level"`
	certdb.CertificateRecord
}

CertRecord extends CFSSL CertificateRecord by adding an enrollment ID to the record

type DB

type DB struct {
	DB SqlxDB
	// Indicates if database was successfully initialized
	IsDBInitialized bool
	CAName          string
	Metrics         *Metrics
}

DB is an adapter for sqlx.DB and implements FabricCADB interface

func New

func New(db SqlxDB, caName string, metrics *Metrics) *DB

New creates an instance of DB

func (*DB) BeginTx

func (db *DB) BeginTx() FabricCATx

BeginTx implements BeginTx method of FabricCADB interface

func (*DB) Close

func (db *DB) Close() error

Close closes db

func (*DB) DriverName

func (db *DB) DriverName() string

DriverName returns database driver name

func (*DB) Exec

func (db *DB) Exec(funcName, query string, args ...interface{}) (sql.Result, error)

Exec executes query

func (*DB) Get

func (db *DB) Get(funcName string, dest interface{}, query string, args ...interface{}) error

Get executes query

func (*DB) IsInitialized

func (db *DB) IsInitialized() bool

IsInitialized returns true if db is initialized, else false

func (*DB) MustBegin

func (db *DB) MustBegin() *sqlx.Tx

MustBegin starts a transaction

func (*DB) NamedExec

func (db *DB) NamedExec(funcName, query string, args interface{}) (sql.Result, error)

NamedExec executes query

func (*DB) PingContext

func (db *DB) PingContext(ctx context.Context) error

PingContext pings the database

func (*DB) Queryx

func (db *DB) Queryx(funcName, query string, args ...interface{}) (*sqlx.Rows, error)

Queryx executes query

func (*DB) Rebind

func (db *DB) Rebind(query string) string

Rebind parses query to properly format query

func (*DB) Select

func (db *DB) Select(funcName string, dest interface{}, query string, args ...interface{}) error

Select performs select sql statement

func (*DB) SetDBInitialized

func (db *DB) SetDBInitialized(b bool)

SetDBInitialized sets the value for Isdbinitialized

func (*DB) SetMaxOpenConns

func (db *DB) SetMaxOpenConns(n int)

SetMaxOpenConns sets number of max open connections

type FabricCADB

type FabricCADB interface {
	IsInitialized() bool
	SetDBInitialized(bool)
	// BeginTx has same behavior as MustBegin except it returns FabricCATx
	// instead of *sqlx.Tx
	BeginTx() FabricCATx
	DriverName() string

	Select(funcName string, dest interface{}, query string, args ...interface{}) error
	Exec(funcName, query string, args ...interface{}) (sql.Result, error)
	NamedExec(funcName, query string, arg interface{}) (sql.Result, error)
	Get(funcName string, dest interface{}, query string, args ...interface{}) error
	Queryx(funcName, query string, args ...interface{}) (*sqlx.Rows, error)
	Rebind(query string) string
	MustBegin() *sqlx.Tx
	Close() error
	SetMaxOpenConns(n int)
	PingContext(ctx context.Context) error
}

FabricCADB is the interface that wrapper off SqlxDB

type FabricCATx

type FabricCATx interface {
	Select(funcName string, dest interface{}, query string, args ...interface{}) error
	Exec(funcName, query string, args ...interface{}) (sql.Result, error)
	Queryx(funcName, query string, args ...interface{}) (*sqlx.Rows, error)
	Get(funcName string, dest interface{}, query string, args ...interface{}) error
	Rebind(query string) string
	Commit(funcName string) error
	Rollback(funcName string) error
}

FabricCATx is the interface with functions implemented by sqlx.Tx object that are used by Fabric CA server

type Metrics

type Metrics struct {
	// APICounter keeps track of number of times a database API is called
	APICounter metrics.Counter
	// APIDuration keeps track of time taken for request to complete to a database API
	APIDuration metrics.Histogram
}

Metrics is the set of meters for the database

type Migrator

type Migrator interface {
	MigrateUsersTable() error
	MigrateCertificatesTable() error
	MigrateAffiliationsTable() error
	MigrateCredentialsTable() error
	MigrateRAInfoTable() error
	MigrateNoncesTable() error
	Rollback() error
	Commit() error
}

Migrator is the interface that defines a migrator

type SqlxDB

type SqlxDB interface {
	DriverName() string
	Select(dest interface{}, query string, args ...interface{}) error
	Exec(query string, args ...interface{}) (sql.Result, error)
	NamedExec(query string, arg interface{}) (sql.Result, error)
	Get(dest interface{}, query string, args ...interface{}) error
	Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
	Rebind(query string) string
	MustBegin() *sqlx.Tx
	Close() error
	SetMaxOpenConns(n int)
	PingContext(ctx context.Context) error
}

SqlxDB is the interface with functions implemented by sqlx.DB object that are used by Fabric CA server

type SqlxTx

type SqlxTx interface {
	Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
	Get(dest interface{}, query string, args ...interface{}) error
	Select(dest interface{}, query string, args ...interface{}) error
	Rebind(query string) string
	Exec(query string, args ...interface{}) (sql.Result, error)
	Commit() error
	Rollback() error
}

SqlxTx is the contract with sqlx

type TX

type TX struct {
	TX     SqlxTx
	Record record
}

TX is the database transaction

func (*TX) Commit

func (tx *TX) Commit(funcName string) error

Commit commits the transaction

func (*TX) Exec

func (tx *TX) Exec(funcName, query string, args ...interface{}) (sql.Result, error)

Exec executes query

func (*TX) Get

func (tx *TX) Get(funcName string, dest interface{}, query string, args ...interface{}) error

Get executes query

func (*TX) Queryx

func (tx *TX) Queryx(funcName, query string, args ...interface{}) (*sqlx.Rows, error)

Queryx executes query

func (*TX) Rebind

func (tx *TX) Rebind(query string) string

Rebind rebinds the query

func (*TX) Rollback

func (tx *TX) Rollback(funcName string) error

Rollback roll backs the transaction

func (*TX) Select

func (tx *TX) Select(funcName string, dest interface{}, query string, args ...interface{}) error

Select performs select sql statement

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL