Skip to content

Commit

Permalink
Export SybDriver and update dependecies
Browse files Browse the repository at this point in the history
  • Loading branch information
thda committed Jul 2, 2020
1 parent e6c1d53 commit 1a2d89f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
35 changes: 29 additions & 6 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,18 @@ type ErrorHandler interface {
SetErrorhandler(fn func(s SybError) bool)
}

// register the driver
type sybDriver struct {
// SybDriver is the driver implementing driver.Driver interface
type SybDriver struct {
sync.Mutex
IsError func(s SybError) bool
}

var sybDriverInstance = &sybDriver{}
var sybDriverInstance = &SybDriver{}

func (d *sybDriver) Open(dsn string) (driver.Conn, error) {
// Open opens a connection to the server.
// See https://github.com/thda/tds#connection-string for the dsn formatting.
// It also set the custum error handler if any.
func (d *SybDriver) Open(dsn string) (driver.Conn, error) {
conn, err := NewConn(dsn)
if d.IsError != nil {
conn.SetErrorhandler(d.IsError)
Expand All @@ -187,7 +190,27 @@ func (d *sybDriver) Open(dsn string) (driver.Conn, error) {
// SetErrorhandler allows setting a custom error handler.
// The function shall accept an SQL Message and return a boolean
// indicating if this message is indeed a critical error.
func (d *sybDriver) SetErrorhandler(fn func(s SybError) bool) {
//
// Example:
//
// // Print showplan messages
// conn.Driver().(tds.ErrorHandler).SetErrorhandler(func(m tds.SybError) bool {
// if m.Severity == 10 {
// if (m.MsgNumber >= 3612 && m.MsgNumber <= 3615) ||
// (m.MsgNumber >= 6201 && m.MsgNumber <= 6299) ||
// (m.MsgNumber >= 10201 && m.MsgNumber <= 10299) {
// fmt.Printf(m.Message)
// } else {
// fmt.Println(strings.TrimRight(m.Message, "\n"))
// }
// }
//
// if m.Severity > 10 {
// fmt.Print(m)
// }
// return m.Severity > 10
// })
func (d *SybDriver) SetErrorhandler(fn func(s SybError) bool) {
d.Lock()
defer d.Unlock()
d.IsError = fn
Expand All @@ -198,7 +221,7 @@ func init() {
sql.Register("tds", sybDriverInstance)
}

var _ driver.Driver = (*sybDriver)(nil)
var _ driver.Driver = (*SybDriver)(nil)

// empty objects to return on error
// Make sure the session is not nil to avoid nil pointers
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ require (
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/xo/tblfmt v0.0.0-20200425011819-c88dde44428d
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 // indirect
golang.org/x/text v0.3.2
golang.org/x/net v0.0.0-20200625001655-4c5254603344
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
golang.org/x/text v0.3.3
)

go 1.13
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ github.com/xo/tblfmt v0.0.0-20191214134155-4c686ae34009/go.mod h1:+aPrd+upGAjb50
github.com/xo/tblfmt v0.0.0-20200425011819-c88dde44428d h1:c03VeZ8zxmxq7lnV8ZYAQuXkALYcN3vY/NBJACXyodY=
github.com/xo/tblfmt v0.0.0-20200425011819-c88dde44428d/go.mod h1:uavzhZXPco8lx36715T/HQW0XLX0km6wUo53GIEozqU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 h1:Dho5nD6R3PcW2SH1or8vS0dszDaXRxIw55lBX7XiE5g=
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

0 comments on commit 1a2d89f

Please sign in to comment.