2022-12-21 18:14:13 +01:00
|
|
|
package dbtools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-01-06 00:39:21 +01:00
|
|
|
type DBLogger struct {
|
|
|
|
Ident string
|
|
|
|
}
|
2022-12-21 18:14:13 +01:00
|
|
|
|
|
|
|
func (l DBLogger) PrePing(ctx context.Context) error {
|
|
|
|
log.Debug().Msg("[SQL-PING]")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PreTxBegin(ctx context.Context, txid uint16) error {
|
2023-01-06 00:39:21 +01:00
|
|
|
log.Debug().Msg(fmt.Sprintf("[SQL-TX<%s|%d>-START]", l.Ident, txid))
|
2022-12-21 18:14:13 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PreTxCommit(txid uint16) error {
|
2023-01-06 00:39:21 +01:00
|
|
|
log.Debug().Msg(fmt.Sprintf("[SQL-TX<%s|%d>-COMMIT]", l.Ident, txid))
|
2022-12-21 18:14:13 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PreTxRollback(txid uint16) error {
|
2023-01-06 00:39:21 +01:00
|
|
|
log.Debug().Msg(fmt.Sprintf("[SQL-TX<%s|%d>-ROLLBACK]", l.Ident, txid))
|
2022-12-21 18:14:13 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PreQuery(ctx context.Context, txID *uint16, sql *string, params *sq.PP) error {
|
|
|
|
if txID == nil {
|
2023-01-06 00:39:21 +01:00
|
|
|
log.Debug().Msg(fmt.Sprintf("[SQL<%s>-QUERY] %s", l.Ident, fmtSQLPrint(*sql)))
|
2022-12-21 18:14:13 +01:00
|
|
|
} else {
|
2023-01-06 00:39:21 +01:00
|
|
|
log.Debug().Msg(fmt.Sprintf("[SQL-TX<%s|%d>-QUERY] %s", l.Ident, *txID, fmtSQLPrint(*sql)))
|
2022-12-21 18:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PreExec(ctx context.Context, txID *uint16, sql *string, params *sq.PP) error {
|
|
|
|
if txID == nil {
|
2023-01-06 00:39:21 +01:00
|
|
|
log.Debug().Msg(fmt.Sprintf("[SQL-<%s>-EXEC] %s", l.Ident, fmtSQLPrint(*sql)))
|
2022-12-21 18:14:13 +01:00
|
|
|
} else {
|
2023-01-06 00:39:21 +01:00
|
|
|
log.Debug().Msg(fmt.Sprintf("[SQL-TX<%s|%d>-EXEC] %s", l.Ident, *txID, fmtSQLPrint(*sql)))
|
2022-12-21 18:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PostPing(result error) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PostTxBegin(txid uint16, result error) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PostTxCommit(txid uint16, result error) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PostTxRollback(txid uint16, result error) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PostQuery(txID *uint16, sqlOriginal string, sqlReal string, params sq.PP) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DBLogger) PostExec(txID *uint16, sqlOriginal string, sqlReal string, params sq.PP) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
func fmtSQLPrint(sql string) string {
|
|
|
|
if strings.Contains(sql, ";") {
|
|
|
|
return "(...multi...)"
|
|
|
|
}
|
|
|
|
|
|
|
|
sql = strings.ReplaceAll(sql, "\r", "")
|
|
|
|
sql = strings.ReplaceAll(sql, "\n", " ")
|
|
|
|
|
|
|
|
return sql
|
|
|
|
}
|