2025-01-17 14:34:48 +00:00
|
|
|
/*
|
2025-02-05 09:05:52 +00:00
|
|
|
Package transaction provides mockable interfaces of sql package struct types.
|
2025-01-17 14:34:48 +00:00
|
|
|
*/
|
|
|
|
package transaction
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
)
|
|
|
|
|
2025-02-05 09:05:52 +00:00
|
|
|
// Client is an interface over a subset of sql.Tx methods
|
|
|
|
// rationale 1: explicitly forbid direct access to Commit and Rollback functionality
|
|
|
|
// as that is exclusively dealt with by WithTransaction in ../db
|
|
|
|
// rationale 2: allow mocking
|
|
|
|
type Client interface {
|
2025-01-17 14:34:48 +00:00
|
|
|
Exec(query string, args ...any) (sql.Result, error)
|
2025-02-05 09:05:52 +00:00
|
|
|
Stmt(stmt *sql.Stmt) Stmt
|
2025-01-17 14:34:48 +00:00
|
|
|
}
|
|
|
|
|
2025-02-05 09:05:52 +00:00
|
|
|
// client is the main implementation of Client, delegates to sql.Tx
|
|
|
|
// other implementations exist for testing purposes
|
|
|
|
type client struct {
|
|
|
|
tx *sql.Tx
|
2025-01-17 14:34:48 +00:00
|
|
|
}
|
|
|
|
|
2025-02-05 09:05:52 +00:00
|
|
|
func NewClient(tx *sql.Tx) Client {
|
|
|
|
return &client{tx: tx}
|
2025-01-17 14:34:48 +00:00
|
|
|
}
|
|
|
|
|
2025-02-05 09:05:52 +00:00
|
|
|
func (c client) Exec(query string, args ...any) (sql.Result, error) {
|
|
|
|
return c.tx.Exec(query, args...)
|
2025-01-17 14:34:48 +00:00
|
|
|
}
|
|
|
|
|
2025-02-05 09:05:52 +00:00
|
|
|
func (c client) Stmt(stmt *sql.Stmt) Stmt {
|
|
|
|
return c.tx.Stmt(stmt)
|
2025-01-17 14:34:48 +00:00
|
|
|
}
|
|
|
|
|
2025-02-05 09:05:52 +00:00
|
|
|
// Stmt is an interface over a subset of sql.Stmt methods
|
|
|
|
// rationale: allow mocking
|
|
|
|
type Stmt interface {
|
|
|
|
Exec(args ...any) (sql.Result, error)
|
|
|
|
Query(args ...any) (*sql.Rows, error)
|
|
|
|
QueryContext(ctx context.Context, args ...any) (*sql.Rows, error)
|
2025-01-17 14:34:48 +00:00
|
|
|
}
|