/
driver.go
49 lines (41 loc) · 1.08 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package dbi
import (
"os"
"http"
)
// API to be implemented by all drivers (i.e. go-dbd-*)
type Driver interface {
// Get a connection to the database identified by the given values.
Connect(url *http.URL) (Connection, os.Error)
}
// Maps driver names to their implementation.
var drivers map[string]Driver
// Register a driver implementation.
func AddDriver(name string, driver Driver) {
checkDriversReady()
drivers[name] = driver
}
// Connect to the database specified by dsn.
func Connect(dsn string) (Connection, os.Error) {
url, err := http.ParseURLReference(dsn)
if err != nil {
return nil, os.NewError("Invalid DSN URL: " + dsn)
}
driver, found := drivers[url.Scheme]
if !found {
return nil, os.NewError("No driver found: " + url.Scheme)
}
return driver.Connect(url)
}
func checkDriversReady() {
if drivers == nil {
drivers = make(map[string]Driver)
}
}
func init() {
//
// Previous calls to AddDriver() by imported drivers may have
// initialized drivers already.
//
checkDriversReady()
}