-
Notifications
You must be signed in to change notification settings - Fork 436
/
mockdriver.go
163 lines (136 loc) · 3.83 KB
/
mockdriver.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
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
157
158
159
160
161
162
163
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package internal
import (
"context"
"database/sql/driver"
"io"
)
// MockDriver implements a mock driver that captures and stores prepared and executed statements
type MockDriver struct {
Prepared []string
Executed []string
// Hook is an optional function to run during a DB operation
Hook func()
}
// Open implements the Conn interface
func (d *MockDriver) Open(_ string) (driver.Conn, error) {
if d.Hook != nil {
d.Hook()
}
return &mockConn{driver: d}, nil
}
type mockConn struct {
driver *MockDriver
}
// Prepare implements the driver.Conn interface
func (m *mockConn) Prepare(query string) (driver.Stmt, error) {
m.driver.Prepared = append(m.driver.Prepared, query)
if m.driver.Hook != nil {
m.driver.Hook()
}
return &mockStmt{stmt: query, driver: m.driver}, nil
}
// QueryContext implements the QueryerContext interface
func (m *mockConn) QueryContext(_ context.Context, query string, _ []driver.NamedValue) (driver.Rows, error) {
m.driver.Executed = append(m.driver.Executed, query)
if m.driver.Hook != nil {
m.driver.Hook()
}
return &rows{}, nil
}
// ExecContext implements the ExecerContext interface
func (m *mockConn) ExecContext(_ context.Context, query string, _ []driver.NamedValue) (driver.Result, error) {
m.driver.Executed = append(m.driver.Executed, query)
if m.driver.Hook != nil {
m.driver.Hook()
}
return &mockResult{}, nil
}
// Close implements the Conn interface
func (m *mockConn) Close() (err error) {
return nil
}
// Begin implements the Conn interface
func (m *mockConn) Begin() (driver.Tx, error) {
if m.driver.Hook != nil {
m.driver.Hook()
}
return &mockTx{driver: m.driver}, nil
}
type rows struct{}
// Columns implements the Rows interface
func (r *rows) Columns() []string {
return []string{}
}
// Close implements the Rows interface
func (r *rows) Close() error {
return nil
}
// Next implements the Rows interface
func (r *rows) Next(_ []driver.Value) error {
return io.EOF
}
type mockTx struct {
driver *MockDriver
}
// Commit implements the Tx interface
func (t *mockTx) Commit() error {
if t.driver.Hook != nil {
t.driver.Hook()
}
return nil
}
// Rollback implements the Tx interface
func (t *mockTx) Rollback() error {
return nil
}
type mockStmt struct {
stmt string
driver *MockDriver
}
// Close implements the Stmt interface
func (s *mockStmt) Close() error {
return nil
}
// NumInput implements the Stmt interface
func (s *mockStmt) NumInput() int {
return 0
}
// Exec implements the Stmt interface
func (s *mockStmt) Exec(_ []driver.Value) (driver.Result, error) {
s.driver.Executed = append(s.driver.Executed, s.stmt)
return &mockResult{}, nil
}
// Query implements the Stmt interface
func (s *mockStmt) Query(_ []driver.Value) (driver.Rows, error) {
s.driver.Executed = append(s.driver.Executed, s.stmt)
return &rows{}, nil
}
// ExecContext implements the StmtExecContext interface
func (s *mockStmt) ExecContext(_ context.Context, _ []driver.NamedValue) (driver.Result, error) {
s.driver.Executed = append(s.driver.Executed, s.stmt)
if s.driver.Hook != nil {
s.driver.Hook()
}
return &mockResult{}, nil
}
// QueryContext implements the StmtQueryContext interface
func (s *mockStmt) QueryContext(_ context.Context, _ []driver.NamedValue) (driver.Rows, error) {
s.driver.Executed = append(s.driver.Executed, s.stmt)
if s.driver.Hook != nil {
s.driver.Hook()
}
return &rows{}, nil
}
type mockResult struct{}
// LastInsertId implements the Result interface
func (r *mockResult) LastInsertId() (int64, error) {
return 0, nil
}
// RowsAffected implements the Result interface
func (r *mockResult) RowsAffected() (int64, error) {
return 0, nil
}