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}", } )
func CurrentDBLevels(db FabricCADB) (*util.Levels, error)
CurrentDBLevels returns current levels from the database
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
AffiliationRecord defines the properties of an affiliation
type AffiliationRecord struct { ID int `db:"id"` Name string `db:"name"` Prekey string `db:"prekey"` Level int `db:"level"` }
CertRecord extends CFSSL CertificateRecord by adding an enrollment ID to the record
type CertRecord struct { ID string `db:"id"` Level int `db:"level"` certdb.CertificateRecord }
DB is an adapter for sqlx.DB and implements FabricCADB interface
type DB struct { DB SqlxDB // Indicates if database was successfully initialized IsDBInitialized bool CAName string Metrics Metrics }
func New(db SqlxDB, caName string, metricsProvider metrics.Provider) *DB
New creates an instance of DB
func (db *DB) BeginTx() FabricCATx
BeginTx implements BeginTx method of FabricCADB interface
func (db *DB) Close() error
Close closes db
func (db *DB) DriverName() string
DriverName returns database driver name
func (db *DB) Exec(funcName, query string, args ...interface{}) (sql.Result, error)
Exec executes query
func (db *DB) Get(funcName string, dest interface{}, query string, args ...interface{}) error
Get executes query
func (db *DB) IsInitialized() bool
IsInitialized returns true if db is intialized, else false
func (db *DB) MustBegin() *sqlx.Tx
MustBegin starts a transaction
func (db *DB) NamedExec(funcName, query string, args interface{}) (sql.Result, error)
NamedExec executes query
func (db *DB) PingContext(ctx context.Context) error
PingContext pings the database
func (db *DB) Queryx(funcName, query string, args ...interface{}) (*sqlx.Rows, error)
Queryx executes query
func (db *DB) Rebind(query string) string
Rebind parses query to properly format query
func (db *DB) Select(funcName string, dest interface{}, query string, args ...interface{}) error
Select performs select sql statement
func (db *DB) SetDBInitialized(b bool)
SetDBInitialized sets the value for Isdbinitialized
func (db *DB) SetMaxOpenConns(n int)
SetMaxOpenConns sets number of max open connections
FabricCADB is the interface that wrapper off SqlxDB
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 }
FabricCATx is the interface with functions implemented by sqlx.Tx object that are used by Fabric CA server
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 }
Metrics is the set of meters for the database
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 }
Migrator is the interface that defines a migrator
type Migrator interface { MigrateUsersTable() error MigrateCertificatesTable() error MigrateAffiliationsTable() error MigrateCredentialsTable() error MigrateRAInfoTable() error MigrateNoncesTable() error Rollback() error Commit() error }
SqlxDB is the interface with functions implemented by sqlx.DB object that are used by Fabric CA server
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 }
SqlxTx is the contract with sqlx
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 }
TX is the database transaction
type TX struct { TX SqlxTx Record record }
func (tx *TX) Commit(funcName string) error
Commit commits the transaction
func (tx *TX) Exec(funcName, query string, args ...interface{}) (sql.Result, error)
Exec executes query
func (tx *TX) Get(funcName string, dest interface{}, query string, args ...interface{}) error
Get executes query
func (tx *TX) Queryx(funcName, query string, args ...interface{}) (*sqlx.Rows, error)
Queryx executes query
func (tx *TX) Rebind(query string) string
Rebind rebinds the query
func (tx *TX) Rollback(funcName string) error
Rollback roll backs the transaction
func (tx *TX) Select(funcName string, dest interface{}, query string, args ...interface{}) error
Select performs select sql statement