/
driver.go
128 lines (100 loc) · 2.88 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package sqlite
import (
"context"
"database/sql/driver"
"io"
"sync"
)
var _ driver.DriverContext = &driverContextWithMutex{}
var _ driver.Driver = &driverContextWithMutex{}
type driverContextWithMutex struct {
driver.DriverContext
*sync.Mutex
}
func (c *driverContextWithMutex) Driver() driver.Driver {
return c
}
func (c *driverContextWithMutex) Open(name string) (driver.Conn, error) {
cc, err := c.OpenConnector(name)
if err != nil {
return nil, err
}
return cc.Connect(context.Background())
}
func (c *driverContextWithMutex) OpenConnector(name string) (driver.Connector, error) {
cc, err := c.DriverContext.OpenConnector(name)
if err != nil {
return nil, nil
}
return &connectorWithMutex{
driverContextWithMutex: c,
Connector: cc,
}, nil
}
var _ driver.Connector = &connectorWithMutex{}
var _ io.Closer = &connectorWithMutex{}
type connectorWithMutex struct {
*driverContextWithMutex
Connector driver.Connector
}
func (c *connectorWithMutex) Close() error {
if cc, ok := c.Connector.(io.Closer); ok {
return cc.Close()
}
return nil
}
func (c *connectorWithMutex) Connect(ctx context.Context) (driver.Conn, error) {
conn, err := c.Connector.Connect(ctx)
if err != nil {
return nil, err
}
return &connWithMutex{Mutex: c.Mutex, Conn: conn}, nil
}
var _ driver.QueryerContext = &connWithMutex{}
var _ driver.ExecerContext = &connWithMutex{}
var _ driver.ConnBeginTx = &connWithMutex{}
type connWithMutex struct {
*sync.Mutex
driver.Conn
}
func (c *connWithMutex) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
c.Lock()
defer c.Unlock()
return c.Conn.(driver.ExecerContext).ExecContext(ctx, query, args)
}
func (c *connWithMutex) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
c.Lock()
defer c.Unlock()
return c.Conn.(driver.QueryerContext).QueryContext(ctx, query, args)
}
func (c *connWithMutex) BeginTx(ctx context.Context, options driver.TxOptions) (driver.Tx, error) {
c.Lock()
defer c.Unlock()
tx, err := c.Conn.(driver.ConnBeginTx).BeginTx(ctx, options)
if err != nil {
return nil, err
}
return &txWithMutex{Mutex: c.Mutex, tx: tx}, nil
}
var _ driver.QueryerContext = &txWithMutex{}
var _ driver.ExecerContext = &txWithMutex{}
type txWithMutex struct {
*sync.Mutex
tx driver.Tx
}
func (c *txWithMutex) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
return c.tx.(driver.ExecerContext).ExecContext(ctx, query, args)
}
func (c *txWithMutex) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
return c.tx.(driver.QueryerContext).QueryContext(ctx, query, args)
}
func (c *txWithMutex) Commit() error {
c.Lock()
defer c.Unlock()
return c.tx.Commit()
}
func (c *txWithMutex) Rollback() error {
c.Lock()
defer c.Unlock()
return c.tx.Rollback()
}