Skip to content

Commit 6e538a9

Browse files
committed
All DB calls now take the context.
1 parent 0090397 commit 6e538a9

25 files changed

+223
-237
lines changed

binding_connection_pool_adapter.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package sqldb
22

3+
import (
4+
"context"
5+
)
6+
37
type BindingConnectionPoolAdapter struct {
48
inner ConnectionPool
59
selector BindingSelector
@@ -14,11 +18,11 @@ func NewBindingConnectionPoolAdapter(actual ConnectionPool, panicOnBindError boo
1418
}
1519
}
1620

17-
func (this *BindingConnectionPoolAdapter) Ping() error {
18-
return this.inner.Ping()
21+
func (this *BindingConnectionPoolAdapter) Ping(ctx context.Context) error {
22+
return this.inner.Ping(ctx)
1923
}
20-
func (this *BindingConnectionPoolAdapter) BeginTransaction() (BindingTransaction, error) {
21-
if tx, err := this.inner.BeginTransaction(); err == nil {
24+
func (this *BindingConnectionPoolAdapter) BeginTransaction(ctx context.Context) (BindingTransaction, error) {
25+
if tx, err := this.inner.BeginTransaction(ctx); err == nil {
2226
return NewBindingTransactionAdapter(tx, this.panicOnBindError), nil
2327
} else {
2428
return nil, err
@@ -28,10 +32,10 @@ func (this *BindingConnectionPoolAdapter) Close() error {
2832
return this.inner.Close()
2933
}
3034

31-
func (this *BindingConnectionPoolAdapter) Execute(statement string, parameters ...interface{}) (uint64, error) {
32-
return this.inner.Execute(statement, parameters...)
35+
func (this *BindingConnectionPoolAdapter) Execute(ctx context.Context, statement string, parameters ...interface{}) (uint64, error) {
36+
return this.inner.Execute(ctx, statement, parameters...)
3337
}
3438

35-
func (this *BindingConnectionPoolAdapter) BindSelect(binder Binder, statement string, parameters ...interface{}) error {
36-
return this.selector.BindSelect(binder, statement, parameters...)
39+
func (this *BindingConnectionPoolAdapter) BindSelect(ctx context.Context, binder Binder, statement string, parameters ...interface{}) error {
40+
return this.selector.BindSelect(ctx, binder, statement, parameters...)
3741
}

binding_connection_pool_adapter_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sqldb
22

33
import (
4+
"context"
45
"errors"
56
"reflect"
67
"testing"
@@ -30,14 +31,14 @@ func (this *BindingConnectionPoolAdapterFixture) Setup() {
3031
func (this *BindingConnectionPoolAdapterFixture) TestPing() {
3132
this.inner.pingError = errors.New("")
3233

33-
err := this.pool.Ping()
34+
err := this.pool.Ping(context.Background())
3435

3536
this.So(err, should.Equal, this.inner.pingError)
3637
this.So(this.inner.pingCalls, should.Equal, 1)
3738
}
3839

3940
func (this *BindingConnectionPoolAdapterFixture) TestBeginTransaction() {
40-
transaction, err := this.pool.BeginTransaction()
41+
transaction, err := this.pool.BeginTransaction(context.Background())
4142

4243
this.So(transaction, should.NotBeNil)
4344
this.So(reflect.TypeOf(transaction), should.Equal, reflect.TypeOf(&BindingTransactionAdapter{}))
@@ -47,7 +48,7 @@ func (this *BindingConnectionPoolAdapterFixture) TestBeginTransaction() {
4748
func (this *BindingConnectionPoolAdapterFixture) TestBeginFailedTransaction() {
4849
this.inner.transactionError = errors.New("")
4950

50-
transaction, err := this.pool.BeginTransaction()
51+
transaction, err := this.pool.BeginTransaction(context.Background())
5152

5253
this.So(transaction, should.BeNil)
5354
this.So(err, should.Equal, this.inner.transactionError)
@@ -66,7 +67,7 @@ func (this *BindingConnectionPoolAdapterFixture) TestExecute() {
6667
this.inner.executeResult = 42
6768
this.inner.executeError = errors.New("")
6869

69-
affected, err := this.pool.Execute("statement")
70+
affected, err := this.pool.Execute(context.Background(), "statement")
7071

7172
this.So(affected, should.Equal, this.inner.executeResult)
7273
this.So(err, should.Equal, this.inner.executeError)
@@ -77,7 +78,7 @@ func (this *BindingConnectionPoolAdapterFixture) TestExecute() {
7778
func (this *BindingConnectionPoolAdapterFixture) TestBindSelect() {
7879
this.inner.selectError = errors.New("")
7980

80-
err := this.pool.BindSelect(nil, "query", 1, 2, 3)
81+
err := this.pool.BindSelect(context.Background(), nil, "query", 1, 2, 3)
8182

8283
this.So(err, should.Equal, this.inner.selectError)
8384
this.So(this.inner.selectCalls, should.Equal, 1)

binding_selector_adapter.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package sqldb
22

3+
import "context"
4+
35
type BindingSelectorAdapter struct {
46
selector Selector
57
panicOnBindError bool
@@ -9,8 +11,8 @@ func NewBindingSelectorAdapter(selector Selector, panicOnBindError bool) *Bindin
911
return &BindingSelectorAdapter{selector: selector, panicOnBindError: panicOnBindError}
1012
}
1113

12-
func (this *BindingSelectorAdapter) BindSelect(binder Binder, statement string, parameters ...interface{}) error {
13-
result, err := this.selector.Select(statement, parameters...)
14+
func (this *BindingSelectorAdapter) BindSelect(ctx context.Context, binder Binder, statement string, parameters ...interface{}) error {
15+
result, err := this.selector.Select(ctx, statement, parameters...)
1416
if err != nil {
1517
return err
1618
}

binding_selector_adapter_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sqldb
22

33
import (
4+
"context"
45
"errors"
56
"testing"
67

@@ -31,7 +32,7 @@ func (this *BindingSelectorAdapterFixture) Setup() {
3132
func (this *BindingSelectorAdapterFixture) TestFailedSelectReturnsError() {
3233
this.fakeInnerSelector.selectError = errors.New("")
3334

34-
err := this.selector.BindSelect(nil, "query", 1, 2, 3)
35+
err := this.selector.BindSelect(context.Background(), nil, "query", 1, 2, 3)
3536

3637
this.So(err, should.Equal, this.fakeInnerSelector.selectError)
3738
this.So(this.fakeInnerSelector.selects, should.Equal, 1)
@@ -40,7 +41,7 @@ func (this *BindingSelectorAdapterFixture) TestFailedSelectReturnsError() {
4041
}
4142

4243
func (this *BindingSelectorAdapterFixture) TestEmptyResult() {
43-
err := this.selector.BindSelect(nil, "query", 1, 2, 3)
44+
err := this.selector.BindSelect(context.Background(), nil, "query", 1, 2, 3)
4445
this.So(err, should.BeNil)
4546
this.So(this.fakeInnerSelector.selects, should.Equal, 1)
4647
this.So(this.fakeResult.nextCalls, should.Equal, 1)
@@ -51,7 +52,7 @@ func (this *BindingSelectorAdapterFixture) TestResultErrorClosesAndReturnsError(
5152
this.fakeResult.iterations = 1
5253
this.fakeResult.errError = errors.New("")
5354

54-
err := this.selector.BindSelect(nil, "query", 1, 2, 3)
55+
err := this.selector.BindSelect(context.Background(), nil, "query", 1, 2, 3)
5556
this.So(err, should.Equal, this.fakeResult.errError)
5657
this.So(this.fakeInnerSelector.selects, should.Equal, 1)
5758
this.So(this.fakeResult.nextCalls, should.Equal, 1)
@@ -63,7 +64,7 @@ func (this *BindingSelectorAdapterFixture) TestScanErrorClosesAndReturnsError()
6364
this.fakeResult.iterations = 1
6465
this.fakeResult.scanError = errors.New("")
6566

66-
err := this.selector.BindSelect(func(source Scanner) error {
67+
err := this.selector.BindSelect(context.Background(), func(source Scanner) error {
6768
return source.Scan()
6869
}, "query", 1, 2, 3)
6970

@@ -81,7 +82,7 @@ func (this *BindingSelectorAdapterFixture) TestScanErrorClosesAndPanicsWhenConfi
8182
this.fakeResult.scanError = errors.New("")
8283

8384
this.So(func() {
84-
this.selector.BindSelect(func(source Scanner) error {
85+
this.selector.BindSelect(context.Background(), func(source Scanner) error {
8586
return source.Scan()
8687
}, "query", 1, 2, 3)
8788
}, should.Panic)
@@ -97,7 +98,7 @@ type FakeSelector struct {
9798
selectError error
9899
}
99100

100-
func (this *FakeSelector) Select(statement string, parameters ...interface{}) (SelectResult, error) {
101+
func (this *FakeSelector) Select(_ context.Context, statement string, parameters ...interface{}) (SelectResult, error) {
101102
this.selects++
102103
this.statement = statement
103104
this.parameters = parameters

binding_transaction_adapter.go

+7-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
package sqldb
22

3+
import "context"
4+
35
type BindingTransactionAdapter struct {
4-
inner Transaction
6+
Transaction
57
selector BindingSelector
68
}
79

810
func NewBindingTransactionAdapter(actual Transaction, panicOnBindError bool) *BindingTransactionAdapter {
911
return &BindingTransactionAdapter{
10-
inner: actual,
11-
selector: NewBindingSelectorAdapter(actual, panicOnBindError),
12+
Transaction: actual,
13+
selector: NewBindingSelectorAdapter(actual, panicOnBindError),
1214
}
1315
}
1416

15-
func (this *BindingTransactionAdapter) Commit() error {
16-
return this.inner.Commit()
17-
}
18-
19-
func (this *BindingTransactionAdapter) Rollback() error {
20-
return this.inner.Rollback()
21-
}
22-
23-
func (this *BindingTransactionAdapter) Execute(statement string, parameters ...interface{}) (uint64, error) {
24-
return this.inner.Execute(statement, parameters...)
25-
}
26-
27-
func (this *BindingTransactionAdapter) BindSelect(binder Binder, statement string, parameters ...interface{}) error {
28-
return this.selector.BindSelect(binder, statement, parameters...)
17+
func (this *BindingTransactionAdapter) BindSelect(ctx context.Context, binder Binder, statement string, parameters ...interface{}) error {
18+
return this.selector.BindSelect(ctx, binder, statement, parameters...)
2919
}

binding_transaction_adapter_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sqldb
22

33
import (
4+
"context"
45
"errors"
56
"testing"
67

@@ -48,7 +49,7 @@ func (this *BindingTransactionAdapterFixture) TestExecute() {
4849
this.inner.executeResult = 42
4950
this.inner.executeError = errors.New("")
5051

51-
affected, err := this.transaction.Execute("statement")
52+
affected, err := this.transaction.Execute(context.Background(), "statement")
5253

5354
this.So(affected, should.Equal, this.inner.executeResult)
5455
this.So(err, should.Equal, this.inner.executeError)
@@ -59,7 +60,7 @@ func (this *BindingTransactionAdapterFixture) TestExecute() {
5960
func (this *BindingTransactionAdapterFixture) TestBindSelect() {
6061
this.inner.selectError = errors.New("")
6162

62-
err := this.transaction.BindSelect(nil, "query", 1, 2, 3)
63+
err := this.transaction.BindSelect(context.Background(), nil, "query", 1, 2, 3)
6364

6465
this.So(err, should.Equal, this.inner.selectError)
6566
this.So(this.inner.selectCalls, should.Equal, 1)

doc_test.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package sqldb
22

3-
import "strings"
3+
import (
4+
"context"
5+
"strings"
6+
)
47

58
///////////////////////////////////////////////////////////////
69

@@ -28,12 +31,12 @@ type FakeConnectionPool struct {
2831
executeError error
2932
}
3033

31-
func (this *FakeConnectionPool) Ping() error {
34+
func (this *FakeConnectionPool) Ping(_ context.Context) error {
3235
this.pingCalls++
3336
return this.pingError
3437
}
3538

36-
func (this *FakeConnectionPool) BeginTransaction() (Transaction, error) {
39+
func (this *FakeConnectionPool) BeginTransaction(_ context.Context) (Transaction, error) {
3740
this.transactionCalls++
3841
return this.transaction, this.transactionError
3942
}
@@ -43,14 +46,14 @@ func (this *FakeConnectionPool) Close() error {
4346
return this.closeError
4447
}
4548

46-
func (this *FakeConnectionPool) Execute(statement string, parameters ...interface{}) (uint64, error) {
49+
func (this *FakeConnectionPool) Execute(_ context.Context, statement string, parameters ...interface{}) (uint64, error) {
4750
this.executeCalls++
4851
this.executeStatement = statement
4952
this.executeParameters = parameters
5053
return this.executeResult, this.executeError
5154
}
5255

53-
func (this *FakeConnectionPool) Select(statement string, parameters ...interface{}) (SelectResult, error) {
56+
func (this *FakeConnectionPool) Select(_ context.Context, statement string, parameters ...interface{}) (SelectResult, error) {
5457
this.selectCalls++
5558
this.selectStatement = statement
5659
this.selectParameters = parameters
@@ -89,14 +92,14 @@ func (this *FakeTransaction) Rollback() error {
8992
return this.rollbackError
9093
}
9194

92-
func (this *FakeTransaction) Execute(statement string, parameters ...interface{}) (uint64, error) {
95+
func (this *FakeTransaction) Execute(_ context.Context, statement string, parameters ...interface{}) (uint64, error) {
9396
this.executeCalls++
9497
this.executeStatement = statement
9598
this.executeParameters = parameters
9699
return this.executeResult, this.executeError
97100
}
98101

99-
func (this *FakeTransaction) Select(statement string, parameters ...interface{}) (SelectResult, error) {
102+
func (this *FakeTransaction) Select(_ context.Context, statement string, parameters ...interface{}) (SelectResult, error) {
100103
this.selectCalls++
101104
this.selectStatement = statement
102105
this.selectParameters = parameters
@@ -132,7 +135,7 @@ func (this *FakeSelectResult) Close() error {
132135
return this.closeError
133136
}
134137

135-
func (this *FakeSelectResult) Scan(target ...interface{}) error {
138+
func (this *FakeSelectResult) Scan(_ ...interface{}) error {
136139
this.scanCalls++
137140
return this.scanError
138141
}
@@ -146,7 +149,7 @@ type FakeExecutor struct {
146149
parameters [][]interface{}
147150
}
148151

149-
func (this *FakeExecutor) Execute(statement string, parameters ...interface{}) (uint64, error) {
152+
func (this *FakeExecutor) Execute(_ context.Context, statement string, parameters ...interface{}) (uint64, error) {
150153
this.statements = append(this.statements, strings.TrimSpace(statement))
151154
this.parameters = append(this.parameters, parameters)
152155

@@ -184,12 +187,12 @@ type FakeBindingConnectionPool struct {
184187
executeError error
185188
}
186189

187-
func (this *FakeBindingConnectionPool) Ping() error {
190+
func (this *FakeBindingConnectionPool) Ping(_ context.Context) error {
188191
this.pingCalls++
189192
return this.pingError
190193
}
191194

192-
func (this *FakeBindingConnectionPool) BeginTransaction() (BindingTransaction, error) {
195+
func (this *FakeBindingConnectionPool) BeginTransaction(_ context.Context) (BindingTransaction, error) {
193196
this.transactionCalls++
194197
return this.transaction, this.transactionError
195198
}
@@ -199,14 +202,14 @@ func (this *FakeBindingConnectionPool) Close() error {
199202
return this.closeError
200203
}
201204

202-
func (this *FakeBindingConnectionPool) Execute(statement string, parameters ...interface{}) (uint64, error) {
205+
func (this *FakeBindingConnectionPool) Execute(_ context.Context, statement string, parameters ...interface{}) (uint64, error) {
203206
this.executeCalls++
204207
this.executeStatement = statement
205208
this.executeParameters = parameters
206209
return this.executeResult, this.executeError
207210
}
208211

209-
func (this *FakeBindingConnectionPool) BindSelect(binder Binder, statement string, parameters ...interface{}) error {
212+
func (this *FakeBindingConnectionPool) BindSelect(_ context.Context, binder Binder, statement string, parameters ...interface{}) error {
210213
this.selectCalls++
211214
this.selectBinder = binder
212215
this.selectStatement = statement
@@ -227,11 +230,11 @@ func (this *FakeBindingTransaction) Rollback() error {
227230
panic("Not called")
228231
}
229232

230-
func (this *FakeBindingTransaction) Execute(statement string, parameters ...interface{}) (uint64, error) {
233+
func (this *FakeBindingTransaction) Execute(_ context.Context, _ string, _ ...interface{}) (uint64, error) {
231234
panic("Not called")
232235
}
233236

234-
func (this *FakeBindingTransaction) BindSelect(binder Binder, statement string, parameters ...interface{}) error {
237+
func (this *FakeBindingTransaction) BindSelect(_ context.Context, _ Binder, _ string, _ ...interface{}) error {
235238
panic("Not called")
236239
}
237240

0 commit comments

Comments
 (0)