-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
112 lines (103 loc) · 3.36 KB
/
options.go
File metadata and controls
112 lines (103 loc) · 3.36 KB
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
package dbx
import "database/sql"
type (
// options holds configuration settings for transaction creation and behavior.
// It embeds sql.TxOptions to provide standard transaction configuration
// while adding dbx-specific settings.
options struct {
*sql.TxOptions // Standard SQL transaction options (isolation, read-only)
AlwaysCreate bool // Forces creation of new transaction even if one exists in context
}
// Option is a functional option type for configuring transaction behavior.
// Options are applied when creating transactions through Transaction or
// TransactionWithResult functions.
//
// Example:
// err := dbx.Transaction(ctx, db, operation,
// dbx.WithIsolationLevel(sql.LevelSerializable),
// dbx.WithReadOnly(true),
// )
Option func(opts *options)
)
// newOptions creates a new options instance with default values and applies
// the provided option setters. This function is used internally to process
// functional options passed to transaction functions.
//
// Parameters:
// - setters: Slice of Option functions to apply to the options
//
// Returns:
// - *options: Configured options instance with all setters applied
func newOptions(setters []Option) *options {
opts := &options{
TxOptions: &sql.TxOptions{},
}
for _, setter := range setters {
setter(opts)
}
return opts
}
// WithIsolationLevel sets the isolation level for the transaction.
// This option configures how the transaction isolates its operations
// from other concurrent transactions.
//
// Parameters:
// - level: The SQL isolation level (e.g., sql.LevelReadCommitted, sql.LevelSerializable)
//
// Returns:
// - Option: A functional option that can be passed to Transaction functions
//
// Example:
//
// err := dbx.Transaction(ctx, db, operation,
// dbx.WithIsolationLevel(sql.LevelSerializable),
// )
func WithIsolationLevel(level sql.IsolationLevel) Option {
return func(opts *options) {
opts.Isolation = level
}
}
// WithReadOnly sets the read-only flag for the transaction.
// Read-only transactions can provide performance benefits and prevent
// accidental data modifications.
//
// Parameters:
// - readOnly: true to make the transaction read-only, false for read-write
//
// Returns:
// - Option: A functional option that can be passed to Transaction functions
//
// Example:
//
// // Create a read-only transaction for safe data reading
// users, err := dbx.TransactionWithResult(ctx, db, getUsersOperation,
// dbx.WithReadOnly(true),
// )
func WithReadOnly(readOnly bool) Option {
return func(opts *options) {
opts.ReadOnly = readOnly
}
}
// WithNewTransaction forces the creation of a new transaction even if there
// is an existing transaction in the context. This is useful when you need
// a separate transaction scope that can be committed or rolled back
// independently of the outer transaction.
//
// By default, dbx reuses existing transactions found in the context to avoid
// nested transaction issues. Use this option when you explicitly need a new
// transaction boundary.
//
// Returns:
// - Option: A functional option that can be passed to Transaction functions
//
// Example:
//
// // Force a new transaction even if we're already in a transaction
// err := dbx.Transaction(ctx, db, independentOperation,
// dbx.WithNewTransaction(),
// )
func WithNewTransaction() Option {
return func(opts *options) {
opts.AlwaysCreate = true
}
}