github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/btcutil/database/driver.go (about)

     1  // Copyright (c) 2015-2016 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package database
     6  
     7  import (
     8  	"fmt"
     9  )
    10  
    11  // Driver defines a structure for backend drivers to use when they registered
    12  // themselves as a backend which implements the DB interface.
    13  type Driver struct {
    14  	// DbType is the identifier used to uniquely identify a specific
    15  	// database driver.  There can be only one driver with the same name.
    16  	DbType string
    17  
    18  	// Create is the function that will be invoked with all user-specified
    19  	// arguments to create the database.  This function must return
    20  	// ErrDbExists if the database already exists.
    21  	Create func(args ...interface{}) (DB, error)
    22  
    23  	// Open is the function that will be invoked with all user-specified
    24  	// arguments to open the database.  This function must return
    25  	// ErrDbDoesNotExist if the database has not already been created.
    26  	Open func(args ...interface{}) (DB, error)
    27  }
    28  
    29  // driverList holds all of the registered database backends.
    30  var drivers = make(map[string]*Driver)
    31  
    32  // RegisterDriver adds a backend database driver to available interfaces.
    33  // ErrDbTypeRegistered will be retruned if the database type for the driver has
    34  // already been registered.
    35  func RegisterDriver(driver Driver) error {
    36  	if _, exists := drivers[driver.DbType]; exists {
    37  		str := fmt.Sprintf("driver %q is already registered",
    38  			driver.DbType)
    39  		return makeError(ErrDbTypeRegistered, str, nil)
    40  	}
    41  
    42  	drivers[driver.DbType] = &driver
    43  	return nil
    44  }
    45  
    46  // SupportedDrivers returns a slice of strings that represent the database
    47  // drivers that have been registered and are therefore supported.
    48  func SupportedDrivers() []string {
    49  	supportedDBs := make([]string, 0, len(drivers))
    50  	for _, drv := range drivers {
    51  		supportedDBs = append(supportedDBs, drv.DbType)
    52  	}
    53  	return supportedDBs
    54  }
    55  
    56  // Create initializes and opens a database for the specified type.  The
    57  // arguments are specific to the database type driver.  See the documentation
    58  // for the database driver for further details.
    59  //
    60  // ErrDbUnknownType will be returned if the the database type is not registered.
    61  func Create(dbType string, args ...interface{}) (DB, error) {
    62  	drv, exists := drivers[dbType]
    63  	if !exists {
    64  		str := fmt.Sprintf("driver %q is not registered", dbType)
    65  		return nil, makeError(ErrDbUnknownType, str, nil)
    66  	}
    67  
    68  	return drv.Create(args...)
    69  }
    70  
    71  // Open opens an existing database for the specified type.  The arguments are
    72  // specific to the database type driver.  See the documentation for the database
    73  // driver for further details.
    74  //
    75  // ErrDbUnknownType will be returned if the the database type is not registered.
    76  func Open(dbType string, args ...interface{}) (DB, error) {
    77  	drv, exists := drivers[dbType]
    78  	if !exists {
    79  		str := fmt.Sprintf("driver %q is not registered", dbType)
    80  		return nil, makeError(ErrDbUnknownType, str, nil)
    81  	}
    82  
    83  	return drv.Open(args...)
    84  }