driver.go raw

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