-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn.go
More file actions
156 lines (130 loc) · 3 KB
/
conn.go
File metadata and controls
156 lines (130 loc) · 3 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package sqlmock
import (
"context"
"database/sql/driver"
"fmt"
)
// Conn implements driver.Conn and routes operations to queued mock expectations.
type Conn struct {
mock *Mock
txStarted bool
}
// Prepare creates a statement bound to this connection for the provided query.
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
return &Stmt{
query: query,
conn: c,
}, nil
}
// extractArgs converts driver.NamedValue arguments into a plain value slice.
func extractArgs(args []driver.NamedValue) []any {
out := make([]any, len(args))
for i, a := range args {
out[i] = a.Value
}
return out
}
// QueryContext validates and consumes the next query expectation.
//
// When strict transaction mode is enabled, QueryContext requires an active
// transaction started via Begin/BeginTx.
func (c *Conn) QueryContext(
ctx context.Context,
query string,
args []driver.NamedValue,
) (driver.Rows, error) {
m := c.mock
m.mu.Lock()
defer m.mu.Unlock()
if m.strictTx && !c.txStarted {
return nil, errWithoutTx("query")
}
op, err := m.next()
if err != nil {
return nil, err
}
q, ok := op.(*ExpectedQuery)
if !ok {
return nil, fmt.Errorf("expected %T, got QUERY", op)
}
if err := q.match(runtimeOp{
kind: "query",
sql: query,
args: extractArgs(args),
}); err != nil {
return nil, err
}
m.consume()
return q.rows, nil
}
// ExecContext validates and consumes the next exec expectation.
//
// When strict transaction mode is enabled, ExecContext requires an active
// transaction started via Begin/BeginTx.
func (c *Conn) ExecContext(
ctx context.Context,
query string,
args []driver.NamedValue,
) (driver.Result, error) {
m := c.mock
m.mu.Lock()
defer m.mu.Unlock()
if m.strictTx && !c.txStarted {
return nil, errWithoutTx("exec")
}
op, err := m.next()
if err != nil {
return nil, err
}
e, ok := op.(*ExpectedExec)
if !ok {
return nil, fmt.Errorf("expected %T, got EXEC", op)
}
if err := e.match(runtimeOp{
kind: "exec",
sql: query,
args: extractArgs(args),
}); err != nil {
return nil, err
}
m.consume()
if e.err != nil {
return nil, e.err
}
return e.result, nil
}
// Close closes the mock connection.
func (c *Conn) Close() error { return nil }
// Begin starts a transaction using default options.
func (c *Conn) Begin() (driver.Tx, error) {
return c.BeginTx(context.Background(), driver.TxOptions{})
}
// BeginTx validates and consumes the next begin expectation.
//
// On success, BeginTx marks the connection transaction as active and returns a
// Tx handle.
func (c *Conn) BeginTx(
ctx context.Context,
opts driver.TxOptions,
) (driver.Tx, error) {
m := c.mock
m.mu.Lock()
defer m.mu.Unlock()
op, err := m.next()
if err != nil {
return nil, err
}
err = op.match(runtimeOp{kind: "begin"})
if err != nil {
return nil, err
}
m.consume()
c.txStarted = true
return &Tx{conn: c}, nil
}
func errWithoutTx(op string) error {
return fmt.Errorf(
"%s without active transaction. Use WithStrictTx() if this should be transactional",
op,
)
}