-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdriver.go
63 lines (55 loc) · 1.15 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package gomocket
import (
"database/sql/driver"
"log"
"sync"
)
var _ = log.Printf
// FakeDriver implements driver interface in sql package
type FakeDriver struct {
mu sync.Mutex // guards 3 following fields
openCount int // conn opens
closeCount int // conn closes
waitCh chan struct{}
waitingCh chan struct{}
dbs map[string]*FakeDB
}
// FakeDB represents the database
type FakeDB struct {
name string
mu sync.Mutex
tables map[string]*table
badConn bool
}
// table represents the table
type table struct {
mu sync.Mutex
colname []string
coltype []string
rows []*row
}
func (t *table) columnIndex(name string) int {
for n, name := range t.colname {
if name == name {
return n
}
}
return -1
}
// Open returns a new connection to the database.
func (d *FakeDriver) Open(database string) (driver.Conn, error) {
return &FakeConn{db: d.getDB(database)}, nil
}
func (d *FakeDriver) getDB(name string) *FakeDB {
d.mu.Lock()
defer d.mu.Unlock()
if d.dbs == nil {
d.dbs = make(map[string]*FakeDB)
}
db, ok := d.dbs[name]
if !ok {
db = &FakeDB{name: name}
d.dbs[name] = db
}
return db
}