2022-12-21 18:14:13 +01:00
|
|
|
package dbtools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/rs/zerolog/log"
|
2023-01-17 21:14:42 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/rext"
|
2022-12-21 18:14:13 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
2023-01-17 21:14:42 +01:00
|
|
|
"regexp"
|
2022-12-21 18:14:13 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-01-17 21:14:42 +01:00
|
|
|
var rexWhitespaceRun = rext.W(regexp.MustCompile("\\s{2,}"))
|
|
|
|
|
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 {
|
2023-01-17 21:14:42 +01:00
|
|
|
if strings.Contains(strings.TrimRight(sql, ";\r\n\t "), ";") {
|
|
|
|
|
|
|
|
str := "(...multi...)"
|
|
|
|
for _, v := range strings.Split(sql, ";") {
|
|
|
|
|
|
|
|
v = strings.ReplaceAll(v, "\r", "")
|
|
|
|
v = strings.ReplaceAll(v, "\n", " ")
|
|
|
|
v = strings.TrimRight(v, ";")
|
|
|
|
v = strings.TrimSpace(v)
|
|
|
|
v = rexWhitespaceRun.ReplaceAll(v, " ", true)
|
|
|
|
|
|
|
|
str += "\n" + " " + v
|
|
|
|
}
|
|
|
|
return str
|
2022-12-21 18:14:13 +01:00
|
|
|
|
2023-01-17 21:14:42 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
sql = strings.ReplaceAll(sql, "\r", "")
|
|
|
|
sql = strings.ReplaceAll(sql, "\n", " ")
|
|
|
|
sql = rexWhitespaceRun.ReplaceAll(sql, " ", true)
|
|
|
|
|
|
|
|
return sql
|
|
|
|
|
|
|
|
}
|
2022-12-21 18:14:13 +01:00
|
|
|
|
|
|
|
}
|