Add support for automatic query logging.

This commit is contained in:
go-jet 2020-05-10 11:41:07 +02:00
parent 5d742837f1
commit 0d3ec872d6
22 changed files with 152 additions and 13 deletions

View file

@ -1,10 +1,13 @@
package postgres
import (
"context"
"database/sql"
"github.com/go-jet/jet/postgres"
"github.com/go-jet/jet/tests/dbconfig"
_ "github.com/lib/pq"
"github.com/pkg/profile"
"github.com/stretchr/testify/require"
"math/rand"
"os"
"os/exec"
@ -43,3 +46,21 @@ func setTestRoot() {
testRoot = strings.TrimSpace(string(byteArr)) + "/tests/"
}
var loggedSQL string
var loggedSQLArgs []interface{}
var loggedDebugSQL string
func init() {
postgres.SetLogger(func(ctx context.Context, statement postgres.LoggableStatement) {
loggedSQL, loggedSQLArgs = statement.Sql()
loggedDebugSQL = statement.DebugSql()
})
}
func requireLogged(t *testing.T, statement postgres.Statement) {
query, args := statement.Sql()
require.Equal(t, loggedSQL, query)
require.Equal(t, loggedSQLArgs, args)
require.Equal(t, loggedDebugSQL, statement.DebugSql())
}