-
Notifications
You must be signed in to change notification settings - Fork 58
/
sql.go
212 lines (175 loc) · 5.64 KB
/
sql.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package checkers
import (
"context"
"database/sql"
"fmt"
)
//go:generate counterfeiter -o ../fakes/isqlpinger.go . SQLPinger
//go:generate counterfeiter -o ../fakes/isqlqueryer.go . SQLQueryer
//go:generate counterfeiter -o ../fakes/isqlexecer.go . SQLExecer
// SQLPinger is an interface that allows direct pinging of the database
type SQLPinger interface {
PingContext(ctx context.Context) error
}
// SQLQueryer is an interface that allows querying of the database
type SQLQueryer interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}
// SQLExecer is an interface that allows executing of queries in the database
type SQLExecer interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}
// SQLQueryerResultHandler is the BYO function to
// handle the result of an SQL SELECT query
type SQLQueryerResultHandler func(rows *sql.Rows) (bool, error)
// SQLExecerResultHandler is the BYO function
// to handle a database exec result
type SQLExecerResultHandler func(result sql.Result) (bool, error)
// SQLConfig is used for configuring a database check.
// One of the Pinger, Queryer, or Execer fields is required.
//
//
// If Execer is set, it will take precedence over Queryer and Pinger,
// Execer implements the SQLExecer interface in this package.
// The sql.DB and sql.TX structs both implement this interface.
//
// Note that if the Execer is set, then the ExecerResultHandler
// and Query values MUST also be set
//
//
// If Queryer is set, it will take precedence over Pinger.
// SQLQueryer implements the SQLQueryer interface in this package.
// The sql.DB and sql.TX structs both implement this interface.
//
// Note that if the Queryer is set, then the QueryerResultHandler
// and Query values MUST also be set
//
//
// Pinger implements the SQLPinger interface in this package.
// The sql.DB struct implements this interface.
type SQLConfig struct {
// Pinger is the value implementing SQLPinger
Pinger SQLPinger
// Queryer is the value implementing SQLQueryer
Queryer SQLQueryer
// Execer is the value implementing SQLExecer
Execer SQLExecer
// Query is the parameterized SQL query required
// with both Queryer and Execer
Query string
// Params are the SQL query parameters, if any
Params []interface{}
// QueryerResultHandler handles the result of
// the QueryContext function
QueryerResultHandler SQLQueryerResultHandler
// ExecerResultHandler handles the result of
// the ExecContext function
ExecerResultHandler SQLExecerResultHandler
}
// SQL implements the "ICheckable" interface
type SQL struct {
Config *SQLConfig
}
// NewSQL creates a new database checker that can be used for ".AddCheck(s)".
func NewSQL(cfg *SQLConfig) (*SQL, error) {
if err := validateSQLConfig(cfg); err != nil {
return nil, err
}
return &SQL{
Config: cfg,
}, nil
}
// DefaultQueryHandler is the default SQLQueryer result handler
// that assumes one row was returned from the passed query
func DefaultQueryHandler(rows *sql.Rows) (bool, error) {
defer rows.Close()
numRows := 0
for rows.Next() {
numRows++
}
return numRows == 1, nil
}
// DefaultExecHandler is the default SQLExecer result handler
// that assumes one row was affected in the passed query
func DefaultExecHandler(result sql.Result) (bool, error) {
affectedRows, err := result.RowsAffected()
if err != nil {
return false, err
}
return affectedRows == int64(1), nil
}
// this makes sure the sql check is properly configured
func validateSQLConfig(cfg *SQLConfig) error {
if cfg == nil {
return fmt.Errorf("config is required")
}
if cfg.Execer == nil && cfg.Queryer == nil && cfg.Pinger == nil {
return fmt.Errorf("one of Execer, Queryer, or Pinger is required in SQLConfig")
}
if (cfg.Execer != nil || cfg.Queryer != nil) && len(cfg.Query) == 0 {
return fmt.Errorf("SQLConfig.Query is required")
}
return nil
}
// Status is used for performing a database ping against a dependency; it satisfies
// the "ICheckable" interface.
func (s *SQL) Status() (interface{}, error) {
if err := validateSQLConfig(s.Config); err != nil {
return nil, err
}
switch {
// check for SQLExecer first
case s.Config.Execer != nil:
// if the result handler is nil, use the default
if s.Config.ExecerResultHandler == nil {
s.Config.ExecerResultHandler = DefaultExecHandler
}
// run the execer
return s.runExecer()
// check for SQLQueryer next
case s.Config.Queryer != nil:
// if the result handler is nil, use the default
if s.Config.QueryerResultHandler == nil {
s.Config.QueryerResultHandler = DefaultQueryHandler
}
// run the queryer
return s.runQueryer()
// finally, must be a pinger
default:
ctx := context.Background()
return nil, s.Config.Pinger.PingContext(ctx)
}
}
// This will run the execer from the Status func
func (s *SQL) runExecer() (interface{}, error) {
ctx := context.Background()
result, err := s.Config.Execer.ExecContext(ctx, s.Config.Query, s.Config.Params...)
if err != nil {
return nil, err
}
ok, err := s.Config.ExecerResultHandler(result)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("userland exec result handler returned false")
}
return nil, nil
}
// This will run the queryer from the Status func
func (s *SQL) runQueryer() (interface{}, error) {
ctx := context.Background()
rows, err := s.Config.Queryer.QueryContext(ctx, s.Config.Query, s.Config.Params...)
if err != nil {
return nil, err
}
// the BYO result handler is responsible for closing the rows
ok, err := s.Config.QueryerResultHandler(rows)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("userland query result handler returned false")
}
return nil, nil
}