1
0
mirror of https://github.com/nxshock/gron.git synced 2024-11-27 03:41:00 +05:00

Add MS SQL database logging

This commit is contained in:
nxshock 2022-05-14 14:17:52 +05:00
parent 1cbca5b0e3
commit 42a6a94421
4 changed files with 37 additions and 7 deletions

23
db.go
View File

@ -1,7 +1,30 @@
package main
import (
"database/sql"
"io"
_ "github.com/denisenkom/go-mssqldb"
mssql "github.com/denisenkom/go-mssqldb"
_ "github.com/jackc/pgx/stdlib"
"github.com/nxshock/logwriter"
_ "github.com/sijms/go-ora/v2"
)
// openMsSqlDb copies MS SQL Driver because logger can be assigned only for driver
func openMsSqlDb(dataSourceName string, logger io.Writer) (*sql.DB, error) {
// Init logger
lw := logwriter.New(logger)
defer lw.Close()
lw.TimeFormat = config.TimeFormat
driverInstance := &mssql.Driver{} // TODO: check hidden processQueryText field
driverInstance.SetLogger(&LogWriter{lw})
connector, err := driverInstance.OpenConnector(dataSourceName)
if err != nil {
return nil, err
}
return sql.OpenDB(connector), nil
}

1
go.mod
View File

@ -23,6 +23,7 @@ require (
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.10.2 // indirect
github.com/nxshock/logwriter v0.0.0-20220512142427-c44d147b9c36 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect

2
go.sum
View File

@ -34,6 +34,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/nxshock/logwriter v0.0.0-20220512142427-c44d147b9c36 h1:k78aHbRfa8XVaCCe7IN4Kc9SiyMuOMv1RLjqiRx2jx4=
github.com/nxshock/logwriter v0.0.0-20220512142427-c44d147b9c36/go.mod h1:V24Kcz59n5zKF4ilF0E86Wq+H0JVTrStXZU6k61qx+k=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

18
job.go
View File

@ -14,7 +14,6 @@ import (
"github.com/BurntSushi/toml"
formatter "github.com/antonfisher/nested-logrus-formatter"
mssql "github.com/denisenkom/go-mssqldb"
log "github.com/sirupsen/logrus"
)
@ -196,17 +195,22 @@ func (j *Job) runCmd(jobLogFile *os.File) error {
}
func (j *Job) runSql(jobLogFile *os.File) error {
db, err := sql.Open(j.JobConfig.Driver, j.JobConfig.ConnectionString)
var (
db *sql.DB
err error
)
switch j.JobConfig.Driver {
case "mssql", "sqlserver":
db, err = openMsSqlDb(j.JobConfig.ConnectionString, jobLogFile)
default:
db, err = sql.Open(j.JobConfig.Driver, j.JobConfig.ConnectionString)
}
if err != nil {
return err
}
defer db.Close()
msSqlDriver, ok := db.Driver().(*mssql.Driver)
if ok {
msSqlDriver.SetLogger(&logFile{jobLogFile})
}
_, err = db.Exec(j.JobConfig.SqlText)
if err != nil {
return err