2022-12-07 23:21:36 +01:00
|
|
|
package sq
|
|
|
|
|
2022-12-21 15:41:41 +01:00
|
|
|
import "context"
|
|
|
|
|
2022-12-07 23:21:36 +01:00
|
|
|
type Listener interface {
|
2022-12-21 15:41:41 +01:00
|
|
|
PrePing(ctx context.Context) error
|
|
|
|
PreTxBegin(ctx context.Context, txid uint16) error
|
2022-12-21 15:34:59 +01:00
|
|
|
PreTxCommit(txid uint16) error
|
|
|
|
PreTxRollback(txid uint16) error
|
2022-12-21 15:41:41 +01:00
|
|
|
PreQuery(ctx context.Context, txID *uint16, sql *string, params *PP) error
|
|
|
|
PreExec(ctx context.Context, txID *uint16, sql *string, params *PP) error
|
2022-12-21 15:34:59 +01:00
|
|
|
|
|
|
|
PostPing(result error)
|
|
|
|
PostTxBegin(txid uint16, result error)
|
|
|
|
PostTxCommit(txid uint16, result error)
|
|
|
|
PostTxRollback(txid uint16, result error)
|
|
|
|
PostQuery(txID *uint16, sqlOriginal string, sqlReal string, params PP)
|
|
|
|
PostExec(txID *uint16, sqlOriginal string, sqlReal string, params PP)
|
2022-12-07 23:21:36 +01:00
|
|
|
}
|
2024-03-09 14:59:32 +01:00
|
|
|
|
|
|
|
type genListener struct {
|
|
|
|
prePing func(ctx context.Context) error
|
|
|
|
preTxBegin func(ctx context.Context, txid uint16) error
|
|
|
|
preTxCommit func(txid uint16) error
|
|
|
|
preTxRollback func(txid uint16) error
|
|
|
|
preQuery func(ctx context.Context, txID *uint16, sql *string, params *PP) error
|
|
|
|
preExec func(ctx context.Context, txID *uint16, sql *string, params *PP) error
|
|
|
|
postPing func(result error)
|
|
|
|
postTxBegin func(txid uint16, result error)
|
|
|
|
postTxCommit func(txid uint16, result error)
|
|
|
|
postTxRollback func(txid uint16, result error)
|
|
|
|
postQuery func(txID *uint16, sqlOriginal string, sqlReal string, params PP)
|
|
|
|
postExec func(txID *uint16, sqlOriginal string, sqlReal string, params PP)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PrePing(ctx context.Context) error {
|
|
|
|
if g.prePing == nil {
|
|
|
|
return g.prePing(ctx)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PreTxBegin(ctx context.Context, txid uint16) error {
|
|
|
|
if g.preTxBegin == nil {
|
|
|
|
return g.preTxBegin(ctx, txid)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PreTxCommit(txid uint16) error {
|
|
|
|
if g.preTxCommit == nil {
|
|
|
|
return g.preTxCommit(txid)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PreTxRollback(txid uint16) error {
|
|
|
|
if g.preTxRollback == nil {
|
|
|
|
return g.preTxRollback(txid)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PreQuery(ctx context.Context, txID *uint16, sql *string, params *PP) error {
|
|
|
|
if g.preQuery == nil {
|
|
|
|
return g.preQuery(ctx, txID, sql, params)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PreExec(ctx context.Context, txID *uint16, sql *string, params *PP) error {
|
|
|
|
if g.preExec == nil {
|
|
|
|
return g.preExec(ctx, txID, sql, params)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PostPing(result error) {
|
|
|
|
if g.postPing != nil {
|
|
|
|
g.postPing(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PostTxBegin(txid uint16, result error) {
|
|
|
|
if g.postTxBegin != nil {
|
|
|
|
g.postTxBegin(txid, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PostTxCommit(txid uint16, result error) {
|
|
|
|
if g.postTxCommit != nil {
|
|
|
|
g.postTxCommit(txid, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PostTxRollback(txid uint16, result error) {
|
|
|
|
if g.postTxRollback != nil {
|
|
|
|
g.postTxRollback(txid, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PostQuery(txID *uint16, sqlOriginal string, sqlReal string, params PP) {
|
|
|
|
if g.postQuery != nil {
|
|
|
|
g.postQuery(txID, sqlOriginal, sqlReal, params)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genListener) PostExec(txID *uint16, sqlOriginal string, sqlReal string, params PP) {
|
|
|
|
if g.postExec != nil {
|
|
|
|
g.postExec(txID, sqlOriginal, sqlReal, params)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPrePingListener(f func(ctx context.Context) error) Listener {
|
|
|
|
return genListener{prePing: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPreTxBeginListener(f func(ctx context.Context, txid uint16) error) Listener {
|
|
|
|
return genListener{preTxBegin: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPreTxCommitListener(f func(txid uint16) error) Listener {
|
|
|
|
return genListener{preTxCommit: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPreTxRollbackListener(f func(txid uint16) error) Listener {
|
|
|
|
return genListener{preTxRollback: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPreQueryListener(f func(ctx context.Context, txID *uint16, sql *string, params *PP) error) Listener {
|
|
|
|
return genListener{preQuery: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPreExecListener(f func(ctx context.Context, txID *uint16, sql *string, params *PP) error) Listener {
|
|
|
|
return genListener{preExec: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPreListener(f func(ctx context.Context, cmdtype string, txID *uint16, sql *string, params *PP) error) Listener {
|
|
|
|
return genListener{
|
|
|
|
preExec: func(ctx context.Context, txID *uint16, sql *string, params *PP) error {
|
|
|
|
return f(ctx, "EXEC", txID, sql, params)
|
|
|
|
},
|
|
|
|
preQuery: func(ctx context.Context, txID *uint16, sql *string, params *PP) error {
|
|
|
|
return f(ctx, "QUERY", txID, sql, params)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostPingListener(f func(result error)) Listener {
|
|
|
|
return genListener{postPing: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostTxBeginListener(f func(txid uint16, result error)) Listener {
|
|
|
|
return genListener{postTxBegin: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostTxCommitListener(f func(txid uint16, result error)) Listener {
|
|
|
|
return genListener{postTxCommit: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostTxRollbackListener(f func(txid uint16, result error)) Listener {
|
|
|
|
return genListener{postTxRollback: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostQueryListener(f func(txID *uint16, sqlOriginal string, sqlReal string, params PP)) Listener {
|
|
|
|
return genListener{postQuery: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostExecListener(f func(txID *uint16, sqlOriginal string, sqlReal string, params PP)) Listener {
|
|
|
|
return genListener{postExec: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostListener(f func(cmdtype string, txID *uint16, sqlOriginal string, sqlReal string, params PP)) Listener {
|
|
|
|
return genListener{
|
|
|
|
postExec: func(txID *uint16, sqlOriginal string, sqlReal string, params PP) {
|
|
|
|
f("EXEC", txID, sqlOriginal, sqlReal, params)
|
|
|
|
},
|
|
|
|
postQuery: func(txID *uint16, sqlOriginal string, sqlReal string, params PP) {
|
|
|
|
f("QUERY", txID, sqlOriginal, sqlReal, params)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|