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