Add automatic query logger function with additional execution details.
This commit is contained in:
parent
7377e078cd
commit
4955bfc4b5
18 changed files with 266 additions and 59 deletions
|
|
@ -5,12 +5,14 @@ import (
|
|||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/go-jet/jet/v2/internal/utils/throw"
|
||||
"github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/sqlite"
|
||||
"github.com/go-jet/jet/v2/tests/dbconfig"
|
||||
"github.com/stretchr/testify/require"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -63,11 +65,36 @@ var loggedSQL string
|
|||
var loggedSQLArgs []interface{}
|
||||
var loggedDebugSQL string
|
||||
|
||||
var queryInfo sqlite.QueryInfo
|
||||
var callerFile string
|
||||
var callerLine int
|
||||
var callerFunction string
|
||||
|
||||
func init() {
|
||||
sqlite.SetLogger(func(ctx context.Context, statement sqlite.PrintableStatement) {
|
||||
loggedSQL, loggedSQLArgs = statement.Sql()
|
||||
loggedDebugSQL = statement.DebugSql()
|
||||
})
|
||||
|
||||
sqlite.SetQueryLoggerFunc(func(ctx context.Context, info sqlite.QueryInfo) {
|
||||
queryInfo = info
|
||||
callerFile, callerLine, callerFunction = info.Caller()
|
||||
})
|
||||
}
|
||||
|
||||
func requireQueryLogged(t *testing.T, statement postgres.Statement, rowsProcessed int64) {
|
||||
query, args := statement.Sql()
|
||||
queryLogged, argsLogged := queryInfo.Statement.Sql()
|
||||
|
||||
require.Equal(t, query, queryLogged)
|
||||
require.Equal(t, args, argsLogged)
|
||||
require.Equal(t, queryInfo.RowsProcessed, rowsProcessed)
|
||||
|
||||
pc, file, _, _ := runtime.Caller(1)
|
||||
funcDetails := runtime.FuncForPC(pc)
|
||||
require.Equal(t, file, callerFile)
|
||||
require.NotEmpty(t, callerLine)
|
||||
require.Equal(t, funcDetails.Name(), callerFunction)
|
||||
}
|
||||
|
||||
func requireLogged(t *testing.T, statement sqlite.Statement) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue