diff --git a/goextVersion.go b/goextVersion.go index b89eb66..579d450 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.216" +const GoextVersion = "0.0.217" -const GoextVersionTimestamp = "2023-07-27T17:00:53+0200" +const GoextVersionTimestamp = "2023-07-27T17:12:41+0200" diff --git a/sq/transaction.go b/sq/transaction.go index 9216ac4..78c6022 100644 --- a/sq/transaction.go +++ b/sq/transaction.go @@ -7,24 +7,40 @@ import ( "gogs.mikescher.com/BlackForestBytes/goext/langext" ) +type TxStatus string + +const ( + TxStatusInitial TxStatus = "INITIAL" + TxStatusActive TxStatus = "ACTIVE" + TxStatusComitted TxStatus = "COMMITTED" + TxStatusRollback TxStatus = "ROLLBACK" +) + type Tx interface { Rollback() error Commit() error + Status() TxStatus 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 { - tx *sqlx.Tx - id uint16 - lstr []Listener + tx *sqlx.Tx + id uint16 + lstr []Listener + status TxStatus + execCtr int + queryCtr int } func NewTransaction(xtx *sqlx.Tx, txid uint16, lstr []Listener) Tx { return &transaction{ - tx: xtx, - id: txid, - lstr: lstr, + tx: xtx, + id: txid, + lstr: lstr, + status: TxStatusInitial, + execCtr: 0, + queryCtr: 0, } } @@ -38,6 +54,10 @@ func (tx *transaction) Rollback() error { result := tx.tx.Rollback() + if result != nil { + tx.status = TxStatusRollback + } + for _, v := range tx.lstr { v.PostTxRollback(tx.id, result) } @@ -55,6 +75,10 @@ func (tx *transaction) Commit() error { result := tx.tx.Commit() + if result != nil { + tx.status = TxStatusComitted + } + for _, v := range tx.lstr { v.PostTxRollback(tx.id, result) } @@ -73,6 +97,10 @@ func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Re res, err := tx.tx.NamedExecContext(ctx, sqlstr, prep) + if tx.status == TxStatusInitial && err != nil { + tx.status = TxStatusActive + } + for _, v := range tx.lstr { v.PostExec(langext.Ptr(tx.id), origsql, sqlstr, prep) } @@ -94,6 +122,10 @@ func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx rows, err := sqlx.NamedQueryContext(ctx, tx.tx, sqlstr, prep) + if tx.status == TxStatusInitial && err != nil { + tx.status = TxStatusActive + } + for _, v := range tx.lstr { v.PostQuery(langext.Ptr(tx.id), origsql, sqlstr, prep) } @@ -103,3 +135,11 @@ func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx } return rows, nil } + +func (tx *transaction) Status() TxStatus { + return tx.status +} + +func (tx *transaction) Traffic() (int, int) { + return tx.execCtr, tx.queryCtr +}