File tree Expand file tree Collapse file tree 3 files changed +32
-4
lines changed Expand file tree Collapse file tree 3 files changed +32
-4
lines changed Original file line number Diff line number Diff line change 14
14
)
15
15
16
16
var (
17
- conn = sqldb .ConnectionWithError (context .Background (), errors .New ("database connection not initialized" ))
18
- connCtxKey int
19
-
17
+ conn = sqldb .ConnectionWithError (
18
+ context .Background (),
19
+ errors .New ("database connection not initialized" ),
20
+ )
21
+ connCtxKey int
20
22
serializedTransactionCtxKey int
21
23
)
Original file line number Diff line number Diff line change @@ -12,6 +12,32 @@ import (
12
12
"github.com/domonda/go-sqldb"
13
13
)
14
14
15
+ // ValidateWithinTransaction returns sqldb.ErrNotWithinTransaction
16
+ // if the database connection from the context is not a transaction.
17
+ func ValidateWithinTransaction (ctx context.Context ) error {
18
+ conn := Conn (ctx )
19
+ if err := conn .Config ().Err ; err != nil {
20
+ return err
21
+ }
22
+ if ! conn .IsTransaction () {
23
+ return sqldb .ErrNotWithinTransaction
24
+ }
25
+ return nil
26
+ }
27
+
28
+ // ValidateNotWithinTransaction returns sqldb.ErrWithinTransaction
29
+ // if the database connection from the context is a transaction.
30
+ func ValidateNotWithinTransaction (ctx context.Context ) error {
31
+ conn := Conn (ctx )
32
+ if err := conn .Config ().Err ; err != nil {
33
+ return err
34
+ }
35
+ if conn .IsTransaction () {
36
+ return sqldb .ErrWithinTransaction
37
+ }
38
+ return nil
39
+ }
40
+
15
41
// DebugNoTransaction executes nonTxFunc without a database transaction.
16
42
// Useful to temporarely replace Transaction to debug the same code without using a transaction.
17
43
func DebugNoTransaction (ctx context.Context , nonTxFunc func (context.Context ) error ) error {
Original file line number Diff line number Diff line change @@ -69,7 +69,7 @@ func (conn *connection) Stats() sql.DBStats {
69
69
}
70
70
71
71
func (conn * connection ) Config () * sqldb.Config {
72
- return & sqldb.Config {Driver : "mockconn" }
72
+ return & sqldb.Config {Driver : "mockconn" , Host : "localhost" , Database : "mock" }
73
73
}
74
74
75
75
func (conn * connection ) Ping (time.Duration ) error {
You can’t perform that action at this time.
0 commit comments