1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-16 07:18:28 +00:00
Files
steve/pkg/sqlcache/db/transaction/transaction.go
Tom Lebreux b4db257cdb Handle transaction failure due to canceled context.Context (#662)
* Re-order SQL event hooks so events are last

* Add QueryRowContext for single line queries

* Add test case for unknown resource version

* Properly check rows and close it

* More accurate error message when context.Context is canceled

* Re-order test check
2025-06-09 15:39:09 -04:00

46 lines
1.2 KiB
Go

/*
Package transaction provides mockable interfaces of sql package struct types.
*/
package transaction
import (
"context"
"database/sql"
)
// 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 {
Exec(query string, args ...any) (sql.Result, error)
Stmt(stmt *sql.Stmt) Stmt
}
// client is the main implementation of Client, delegates to sql.Tx
// other implementations exist for testing purposes
type client struct {
tx *sql.Tx
}
func NewClient(tx *sql.Tx) Client {
return &client{tx: tx}
}
func (c client) Exec(query string, args ...any) (sql.Result, error) {
return c.tx.Exec(query, args...)
}
func (c client) Stmt(stmt *sql.Stmt) Stmt {
return c.tx.Stmt(stmt)
}
// 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)
QueryRowContext(ctx context.Context, args ...any) *sql.Row
}