Skip to content

Commit 6125795

Browse files
committed
Overhauled wireup.
1 parent 6e538a9 commit 6125795

File tree

3 files changed

+183
-82
lines changed

3 files changed

+183
-82
lines changed

config.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package sqldb
2+
3+
import (
4+
"database/sql"
5+
"time"
6+
)
7+
8+
type configuration struct {
9+
txOptions *sql.TxOptions
10+
splitStatement bool
11+
panicOnBindError bool
12+
stackTraceOnError bool
13+
parameterPrefix string
14+
retrySleep time.Duration
15+
}
16+
17+
func NewPool(handle *sql.DB, options ...option) ConnectionPool {
18+
var config configuration
19+
Options.apply(options...)(&config)
20+
return newPool(handle, config)
21+
}
22+
func NewBindingPool(handle *sql.DB, options ...option) BindingConnectionPool {
23+
var config configuration
24+
Options.apply(options...)(&config)
25+
return newBindingPool(handle, config)
26+
}
27+
func newPool(handle *sql.DB, config configuration) ConnectionPool {
28+
var pool ConnectionPool = NewLibraryConnectionPoolAdapter(handle, config.txOptions)
29+
30+
if config.splitStatement {
31+
pool = NewSplitStatementConnectionPool(pool, config.parameterPrefix)
32+
}
33+
34+
if config.stackTraceOnError {
35+
pool = NewStackTraceConnectionPool(pool)
36+
}
37+
38+
return pool
39+
}
40+
func newBindingPool(handle *sql.DB, config configuration) BindingConnectionPool {
41+
inner := newPool(handle, config)
42+
var pool BindingConnectionPool = NewBindingConnectionPoolAdapter(inner, config.panicOnBindError)
43+
44+
if config.retrySleep > 0 {
45+
pool = NewRetryBindingConnectionPool(pool, config.retrySleep)
46+
}
47+
48+
return pool
49+
}
50+
51+
var Options singleton
52+
53+
type singleton struct{}
54+
type option func(*configuration)
55+
56+
func (singleton) TxOptions(value *sql.TxOptions) option {
57+
return func(this *configuration) { this.txOptions = value }
58+
}
59+
func (singleton) PanicOnBindError(value bool) option {
60+
return func(this *configuration) { this.panicOnBindError = value }
61+
}
62+
func (singleton) MySQL() option {
63+
return func(this *configuration) { this.splitStatement = true; this.parameterPrefix = "?" }
64+
}
65+
func (singleton) ParameterPrefix(value string) option {
66+
return func(this *configuration) { this.parameterPrefix = value }
67+
}
68+
func (singleton) SplitStatement(value bool) option {
69+
return func(this *configuration) { this.splitStatement = value }
70+
}
71+
func (singleton) RetrySleep(value time.Duration) option {
72+
return func(this *configuration) { this.retrySleep = value }
73+
}
74+
func (singleton) StackTraceErrDiagnostics(value bool) option {
75+
return func(this *configuration) { this.stackTraceOnError = value }
76+
}
77+
78+
func (singleton) apply(options ...option) option {
79+
return func(this *configuration) {
80+
for _, option := range Options.defaults(options...) {
81+
option(this)
82+
}
83+
}
84+
}
85+
func (singleton) defaults(options ...option) []option {
86+
var defaultTxOptions = &sql.TxOptions{Isolation: sql.LevelReadCommitted}
87+
const defaultStackTraceErrDiagnostics = true
88+
const defaultPanicOnBindError = true
89+
const defaultSplitStatement = true
90+
const defaultParameterPrefix = "?"
91+
const defaultRetrySleep = 0
92+
93+
return append([]option{
94+
Options.TxOptions(defaultTxOptions),
95+
Options.PanicOnBindError(defaultPanicOnBindError),
96+
Options.StackTraceErrDiagnostics(defaultStackTraceErrDiagnostics),
97+
Options.ParameterPrefix(defaultParameterPrefix),
98+
Options.SplitStatement(defaultSplitStatement),
99+
Options.RetrySleep(defaultRetrySleep),
100+
}, options...)
101+
}

config_tx.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package sqldb
2+
3+
import (
4+
"database/sql"
5+
)
6+
7+
type txConfig struct {
8+
splitStatement bool
9+
panicOnBindError bool
10+
stackTraceOnError bool
11+
parameterPrefix string
12+
}
13+
14+
func NewTransaction(handle *sql.Tx, options ...txOption) Transaction {
15+
var config txConfig
16+
TxOptions.apply(options...)(&config)
17+
return newTx(handle, config)
18+
}
19+
func NewBindingTransaction(handle *sql.Tx, options ...txOption) BindingTransaction {
20+
var config txConfig
21+
TxOptions.apply(options...)(&config)
22+
return newBindingTx(handle, config)
23+
}
24+
func newTx(handle *sql.Tx, config txConfig) Transaction {
25+
var tx Transaction = NewLibraryTransactionAdapter(handle)
26+
27+
if config.splitStatement {
28+
tx = NewSplitStatementTransaction(tx, config.parameterPrefix)
29+
}
30+
31+
if config.stackTraceOnError {
32+
tx = NewStackTraceTransaction(tx)
33+
}
34+
35+
return tx
36+
}
37+
func newBindingTx(handle *sql.Tx, config txConfig) BindingTransaction {
38+
inner := newTx(handle, config)
39+
return NewBindingTransactionAdapter(inner, config.panicOnBindError)
40+
}
41+
42+
var TxOptions txSingleton
43+
44+
type txSingleton struct{}
45+
type txOption func(*txConfig)
46+
47+
func (txSingleton) PanicOnBindError(value bool) txOption {
48+
return func(this *txConfig) { this.panicOnBindError = value }
49+
}
50+
func (txSingleton) MySQL() txOption {
51+
return func(this *txConfig) { this.splitStatement = true; this.parameterPrefix = "?" }
52+
}
53+
func (txSingleton) ParameterPrefix(value string) txOption {
54+
return func(this *txConfig) { this.parameterPrefix = value }
55+
}
56+
func (txSingleton) SplitStatement(value bool) txOption {
57+
return func(this *txConfig) { this.splitStatement = value }
58+
}
59+
func (txSingleton) StackTraceErrDiagnostics(value bool) txOption {
60+
return func(this *txConfig) { this.stackTraceOnError = value }
61+
}
62+
63+
func (txSingleton) apply(txOptions ...txOption) txOption {
64+
return func(this *txConfig) {
65+
for _, txOption := range TxOptions.defaults(txOptions...) {
66+
txOption(this)
67+
}
68+
}
69+
}
70+
func (txSingleton) defaults(txOptions ...txOption) []txOption {
71+
const defaultStackTraceErrDiagnostics = true
72+
const defaultPanicOnBindError = true
73+
const defaultSplitStatement = true
74+
const defaultParameterPrefix = "?"
75+
76+
return append([]txOption{
77+
TxOptions.PanicOnBindError(defaultPanicOnBindError),
78+
TxOptions.StackTraceErrDiagnostics(defaultStackTraceErrDiagnostics),
79+
TxOptions.ParameterPrefix(defaultParameterPrefix),
80+
TxOptions.SplitStatement(defaultSplitStatement),
81+
}, txOptions...)
82+
}

wireup.go

-82
This file was deleted.

0 commit comments

Comments
 (0)