This commit is contained in:
Mike Schwörer 2023-07-27 17:12:41 +02:00
parent 4f871271e8
commit b7003b9ec9
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 48 additions and 8 deletions

View File

@ -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"

View File

@ -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
}