OSDN Git Service

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