git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/dbx/transaction.go (about) 1 package dbx 2 3 import ( 4 "context" 5 "database/sql" 6 7 "github.com/jmoiron/sqlx" 8 ) 9 10 // Transaction is wrapper of `sqlx.Tx` which implements `Tx` 11 type Tx struct { 12 sqlxTx *sqlx.Tx 13 } 14 15 // Commit commits the transaction. 16 func (tx *Tx) Commit() error { 17 return tx.sqlxTx.Commit() 18 } 19 20 // Rollback aborts the transaction. 21 func (tx *Tx) Rollback() error { 22 return tx.sqlxTx.Rollback() 23 } 24 25 // Exec executes a query without returning any rows. The args are for any placeholder parameters in the query. 26 func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) { 27 return tx.sqlxTx.ExecContext(ctx, query, args...) 28 } 29 30 // Get a single record. Any placeholder parameters are replaced with supplied args. An `ErrNoRows` 31 // error is returned if the result set is empty. 32 func (tx *Tx) GetContext(ctx context.Context, dest any, query string, args ...any) error { 33 return tx.sqlxTx.GetContext(ctx, dest, query, args...) 34 } 35 36 // QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder 37 // parameters in the query. 38 func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) { 39 return tx.sqlxTx.QueryContext(ctx, query, args...) 40 } 41 42 // Select an array of records. Any placeholder parameters are replaced with supplied args. 43 func (tx *Tx) Select(ctx context.Context, dest any, query string, args ...any) error { 44 return tx.sqlxTx.SelectContext(ctx, dest, query, args...) 45 } 46 47 func (tx *Tx) QueryRowxContext(ctx context.Context, query string, args ...any) *sqlx.Row { 48 return tx.sqlxTx.QueryRowxContext(ctx, query, args...) 49 } 50 51 func (tx *Tx) QueryxContext(ctx context.Context, query string, args ...any) (*sqlx.Rows, error) { 52 return tx.sqlxTx.QueryxContext(ctx, query, args...) 53 }