-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_tx.go
82 lines (71 loc) · 2.32 KB
/
config_tx.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
package sqldb
import (
"database/sql"
)
type txConfig struct {
splitStatement bool
panicOnBindError bool
stackTraceOnError bool
parameterPrefix string
}
func NewTransaction(handle *sql.Tx, options ...txOption) Transaction {
var config txConfig
TxOptions.apply(options...)(&config)
return newTx(handle, config)
}
func NewBindingTransaction(handle *sql.Tx, options ...txOption) BindingTransaction {
var config txConfig
TxOptions.apply(options...)(&config)
return newBindingTx(handle, config)
}
func newTx(handle *sql.Tx, config txConfig) Transaction {
var tx Transaction = NewLibraryTransactionAdapter(handle)
if config.splitStatement {
tx = NewSplitStatementTransaction(tx, config.parameterPrefix)
}
if config.stackTraceOnError {
tx = NewStackTraceTransaction(tx)
}
return tx
}
func newBindingTx(handle *sql.Tx, config txConfig) BindingTransaction {
inner := newTx(handle, config)
return NewBindingTransactionAdapter(inner, config.panicOnBindError)
}
var TxOptions txSingleton
type txSingleton struct{}
type txOption func(*txConfig)
func (txSingleton) PanicOnBindError(value bool) txOption {
return func(this *txConfig) { this.panicOnBindError = value }
}
func (txSingleton) MySQL() txOption {
return func(this *txConfig) { this.splitStatement = true; this.parameterPrefix = "?" }
}
func (txSingleton) ParameterPrefix(value string) txOption {
return func(this *txConfig) { this.parameterPrefix = value }
}
func (txSingleton) SplitStatement(value bool) txOption {
return func(this *txConfig) { this.splitStatement = value }
}
func (txSingleton) StackTraceErrDiagnostics(value bool) txOption {
return func(this *txConfig) { this.stackTraceOnError = value }
}
func (txSingleton) apply(txOptions ...txOption) txOption {
return func(this *txConfig) {
for _, txOption := range TxOptions.defaults(txOptions...) {
txOption(this)
}
}
}
func (txSingleton) defaults(txOptions ...txOption) []txOption {
const defaultStackTraceErrDiagnostics = true
const defaultPanicOnBindError = true
const defaultSplitStatement = true
const defaultParameterPrefix = "?"
return append([]txOption{
TxOptions.PanicOnBindError(defaultPanicOnBindError),
TxOptions.StackTraceErrDiagnostics(defaultStackTraceErrDiagnostics),
TxOptions.ParameterPrefix(defaultParameterPrefix),
TxOptions.SplitStatement(defaultSplitStatement),
}, txOptions...)
}