4
4
"context"
5
5
"database/sql"
6
6
"fmt"
7
-
8
- "github.com/jmoiron/sqlx"
9
7
)
10
8
11
9
var ErrTxNotFound = fmt .Errorf ("transaction value not found" )
@@ -29,11 +27,21 @@ func TxFromContext(ctx context.Context) (*sql.Tx, error) {
29
27
return tx , nil
30
28
}
31
29
32
- // Executor returns a *sql.Tx or *sqlx.DB to execute a DB command based on the given context.
33
- func Executor (ctx context.Context , db * sqlx.DB ) sqlx.ExecerContext {
30
+ // DB interface contains the common methods between sql.DB and sql.Tx objects
31
+ type DB interface {
32
+ Exec (query string , args ... any ) (sql.Result , error )
33
+ ExecContext (ctx context.Context , query string , args ... any ) (sql.Result , error )
34
+ PrepareContext (ctx context.Context , query string ) (* sql.Stmt , error )
35
+ Query (query string , args ... any ) (* sql.Rows , error )
36
+ QueryContext (ctx context.Context , query string , args ... any ) (* sql.Rows , error )
37
+ QueryRow (query string , args ... any ) * sql.Row
38
+ QueryRowContext (ctx context.Context , query string , args ... any ) * sql.Row
39
+ }
40
+
41
+ // Executor returns a DbOrTx (either *sql.Tx or *sql.DB) to execute a DB command based on the given context.
42
+ func Executor (ctx context.Context , db * sql.DB ) DB {
34
43
if tx , ok := ctx .Value (txSessionKey ).(* sql.Tx ); ok {
35
44
return tx
36
45
}
37
-
38
46
return db
39
47
}
0 commit comments