-
Notifications
You must be signed in to change notification settings - Fork 420
/
mockdriver.go
137 lines (110 loc) · 3.49 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
// 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 "gopkg.in/DataDog/dd-trace-go.v1/contrib/internal/sqltest"
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
}
// Open implements the Conn interface
func (d *MockDriver) Open(name string) (driver.Conn, error) {
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)
return &mockStmt{stmt: query, driver: m.driver}, nil
}
// QueryContext implements the QueryerContext interface
func (m *mockConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
m.driver.Executed = append(m.driver.Executed, query)
return &rows{}, nil
}
// ExecContext implements the ExecerContext interface
func (m *mockConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
m.driver.Executed = append(m.driver.Executed, query)
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) {
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(dest []driver.Value) error {
return io.EOF
}
type mockTx struct {
driver *MockDriver
}
// Commit implements the Tx interface
func (t *mockTx) Commit() error {
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(args []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(args []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(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
s.driver.Executed = append(s.driver.Executed, s.stmt)
return &mockResult{}, nil
}
// QueryContext implements the StmtQueryContext interface
func (s *mockStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
s.driver.Executed = append(s.driver.Executed, s.stmt)
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
}