goext/sq/transaction.go

146 lines
2.7 KiB
Go
Raw Permalink Normal View History

2022-12-07 23:21:36 +01:00
package sq
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)
2023-07-27 17:12:41 +02:00
type TxStatus string
const (
TxStatusInitial TxStatus = "INITIAL"
TxStatusActive TxStatus = "ACTIVE"
TxStatusComitted TxStatus = "COMMITTED"
TxStatusRollback TxStatus = "ROLLBACK"
)
2022-12-07 23:21:36 +01:00
type Tx interface {
Rollback() error
Commit() error
2023-07-27 17:12:41 +02:00
Status() TxStatus
2022-12-07 23:21:36 +01:00
Exec(ctx context.Context, sql string, prep PP) (sql.Result, error)
Query(ctx context.Context, sql string, prep PP) (*sqlx.Rows, error)
}
type transaction struct {
2023-07-27 17:12:41 +02:00
tx *sqlx.Tx
id uint16
lstr []Listener
status TxStatus
execCtr int
queryCtr int
2022-12-07 23:21:36 +01:00
}
2022-12-21 15:34:59 +01:00
func NewTransaction(xtx *sqlx.Tx, txid uint16, lstr []Listener) Tx {
2022-12-07 23:21:36 +01:00
return &transaction{
2023-07-27 17:12:41 +02:00
tx: xtx,
id: txid,
lstr: lstr,
status: TxStatusInitial,
execCtr: 0,
queryCtr: 0,
2022-12-07 23:21:36 +01:00
}
}
func (tx *transaction) Rollback() error {
2022-12-21 15:34:59 +01:00
for _, v := range tx.lstr {
err := v.PreTxRollback(tx.id)
if err != nil {
return err
}
2022-12-07 23:21:36 +01:00
}
2022-12-21 15:34:59 +01:00
result := tx.tx.Rollback()
2023-07-27 17:16:30 +02:00
if result == nil {
2023-07-27 17:12:41 +02:00
tx.status = TxStatusRollback
}
2022-12-21 15:34:59 +01:00
for _, v := range tx.lstr {
v.PostTxRollback(tx.id, result)
}
return result
2022-12-07 23:21:36 +01:00
}
func (tx *transaction) Commit() error {
2022-12-21 15:34:59 +01:00
for _, v := range tx.lstr {
err := v.PreTxCommit(tx.id)
if err != nil {
return err
}
}
result := tx.tx.Commit()
2023-07-27 17:16:30 +02:00
if result == nil {
2023-07-27 17:12:41 +02:00
tx.status = TxStatusComitted
}
2022-12-21 15:34:59 +01:00
for _, v := range tx.lstr {
v.PostTxRollback(tx.id, result)
2022-12-07 23:21:36 +01:00
}
2022-12-21 15:34:59 +01:00
return result
2022-12-07 23:21:36 +01:00
}
2022-12-21 15:34:59 +01:00
func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Result, error) {
origsql := sqlstr
for _, v := range tx.lstr {
2022-12-21 15:41:41 +01:00
err := v.PreExec(ctx, langext.Ptr(tx.id), &sqlstr, &prep)
2022-12-21 15:34:59 +01:00
if err != nil {
return nil, err
}
}
res, err := tx.tx.NamedExecContext(ctx, sqlstr, prep)
2023-07-27 17:16:30 +02:00
if tx.status == TxStatusInitial && err == nil {
2023-07-27 17:12:41 +02:00
tx.status = TxStatusActive
}
2022-12-21 15:34:59 +01:00
for _, v := range tx.lstr {
v.PostExec(langext.Ptr(tx.id), origsql, sqlstr, prep)
2022-12-07 23:21:36 +01:00
}
if err != nil {
return nil, err
}
return res, nil
}
2022-12-21 15:34:59 +01:00
func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx.Rows, error) {
origsql := sqlstr
for _, v := range tx.lstr {
2022-12-21 15:41:41 +01:00
err := v.PreQuery(ctx, langext.Ptr(tx.id), &sqlstr, &prep)
2022-12-21 15:34:59 +01:00
if err != nil {
return nil, err
}
}
rows, err := sqlx.NamedQueryContext(ctx, tx.tx, sqlstr, prep)
2023-07-27 17:16:30 +02:00
if tx.status == TxStatusInitial && err == nil {
2023-07-27 17:12:41 +02:00
tx.status = TxStatusActive
}
2022-12-21 15:34:59 +01:00
for _, v := range tx.lstr {
v.PostQuery(langext.Ptr(tx.id), origsql, sqlstr, prep)
2022-12-07 23:21:36 +01:00
}
if err != nil {
return nil, err
}
return rows, nil
}
2023-07-27 17:12:41 +02:00
func (tx *transaction) Status() TxStatus {
return tx.status
}
func (tx *transaction) Traffic() (int, int) {
return tx.execCtr, tx.queryCtr
}