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:
parent
1cbca5b0e3
commit
42a6a94421
23
db.go
23
db.go
@ -1,7 +1,30 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"io"
|
||||||
|
|
||||||
_ "github.com/denisenkom/go-mssqldb"
|
_ "github.com/denisenkom/go-mssqldb"
|
||||||
|
mssql "github.com/denisenkom/go-mssqldb"
|
||||||
_ "github.com/jackc/pgx/stdlib"
|
_ "github.com/jackc/pgx/stdlib"
|
||||||
|
"github.com/nxshock/logwriter"
|
||||||
_ "github.com/sijms/go-ora/v2"
|
_ "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
1
go.mod
@ -23,6 +23,7 @@ require (
|
|||||||
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
|
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
|
||||||
github.com/kr/pretty v0.1.0 // indirect
|
github.com/kr/pretty v0.1.0 // indirect
|
||||||
github.com/lib/pq v1.10.2 // 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/pkg/errors v0.9.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/shopspring/decimal v1.2.0 // indirect
|
github.com/shopspring/decimal v1.2.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -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 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
|
||||||
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
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/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/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 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
18
job.go
18
job.go
@ -14,7 +14,6 @@ import (
|
|||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
formatter "github.com/antonfisher/nested-logrus-formatter"
|
formatter "github.com/antonfisher/nested-logrus-formatter"
|
||||||
mssql "github.com/denisenkom/go-mssqldb"
|
|
||||||
log "github.com/sirupsen/logrus"
|
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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
msSqlDriver, ok := db.Driver().(*mssql.Driver)
|
|
||||||
if ok {
|
|
||||||
msSqlDriver.SetLogger(&logFile{jobLogFile})
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = db.Exec(j.JobConfig.SqlText)
|
_, err = db.Exec(j.JobConfig.SqlText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user