-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
60 lines (50 loc) · 1.36 KB
/
adapter.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
49
50
51
52
53
54
55
56
57
58
59
60
package gdbc
import (
"sync"
)
type DataSourceNameAdapter interface {
GetDataSourceName(dataSource DataSource) (string, error)
}
var (
dsnAdapterMu sync.RWMutex
dsnAdapters = make(map[string]DataSourceNameAdapter)
driverNameMap = make(map[string]string)
)
func Register(driverName string, driverAliasName string, dsnAdapter DataSourceNameAdapter) {
dsnAdapterMu.Lock()
defer dsnAdapterMu.Unlock()
if driverName == "" || driverAliasName == "" {
panic("sql: Driver and driver alias name cannot be empty")
}
if dsnAdapter == nil {
panic("sql: DSN adapter is nil")
}
_, ok := driverNameMap[driverAliasName]
if ok {
panic("sql: already registered an adapter with the same alias name : " + driverAliasName)
}
driverNameMap[driverAliasName] = driverName
_, ok = dsnAdapters[driverName]
if ok {
panic("sql: you already registered a DSN adapter for driver : " + driverName)
}
dsnAdapters[driverName] = dsnAdapter
}
func GetDataSourceNameAdapter(driverAliasName string) DataSourceNameAdapter {
dsnAdapterMu.RLock()
driverName, ok := driverNameMap[driverAliasName]
dsnAdapterMu.RUnlock()
if !ok {
return nil
}
return dsnAdapters[driverName]
}
func GetDriverName(driverAliasName string) (string, bool) {
dsnAdapterMu.RLock()
driverName, ok := driverNameMap[driverAliasName]
dsnAdapterMu.RUnlock()
if !ok {
return "", false
}
return driverName, true
}