From 7bb1ded93e0b37a73abbaf629c8d27c6ea6245e2 Mon Sep 17 00:00:00 2001 From: "Sushil K. G" <652022+sushilkg@users.noreply.github.com> Date: Thu, 19 May 2022 17:14:18 +0200 Subject: [PATCH 1/2] Implement stringer interface (#746) * Implement stringer interface * Convert to Message type before passing to MsgNotice --- error.go | 12 +++++++++--- queries_go19_test.go | 4 ++-- token.go | 8 ++++---- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/error.go b/error.go index 09964b87..58575c35 100644 --- a/error.go +++ b/error.go @@ -14,7 +14,7 @@ type Error struct { Number int32 State uint8 Class uint8 - Message string + Message Message ServerName string ProcName string LineNo int32 @@ -23,8 +23,14 @@ type Error struct { All []Error } +type Message string + func (e Error) Error() string { - return "mssql: " + e.Message + return "mssql: " + e.Message.String() +} + +func (m Message) String() string { + return string(m) } // SQLErrorNumber returns the SQL Server error number. @@ -41,7 +47,7 @@ func (e Error) SQLErrorClass() uint8 { } func (e Error) SQLErrorMessage() string { - return e.Message + return e.Message.String() } func (e Error) SQLErrorServerName() string { diff --git a/queries_go19_test.go b/queries_go19_test.go index 12371094..0534dce7 100644 --- a/queries_go19_test.go +++ b/queries_go19_test.go @@ -1125,10 +1125,10 @@ func TestMessageQueue(t *testing.T) { active := true msgs := []interface{}{ - sqlexp.MsgNotice{Message: "msg1"}, + sqlexp.MsgNotice{Message: Message("msg1")}, sqlexp.MsgNext{}, sqlexp.MsgRowsAffected{Count: 1}, - sqlexp.MsgNotice{Message: "msg2"}, + sqlexp.MsgNotice{Message: Message("msg2")}, sqlexp.MsgNextResultSet{}, } i := 0 diff --git a/token.go b/token.go index 43039d3d..939e61b5 100644 --- a/token.go +++ b/token.go @@ -601,7 +601,7 @@ func parseError72(r *tdsBuffer) (res Error) { res.Number = r.int32() res.State = r.byte() res.Class = r.byte() - res.Message = r.UsVarChar() + res.Message = Message(r.UsVarChar()) res.ServerName = r.BVarChar() res.ProcName = r.BVarChar() res.LineNo = r.int32() @@ -615,7 +615,7 @@ func parseInfo(r *tdsBuffer) (res Error) { res.Number = r.int32() res.State = r.byte() res.Class = r.byte() - res.Message = r.UsVarChar() + res.Message = Message(r.UsVarChar()) res.ServerName = r.BVarChar() res.ProcName = r.BVarChar() res.LineNo = r.int32() @@ -773,7 +773,7 @@ func processSingleResponse(ctx context.Context, sess *tdsSession, ch chan tokenS } errs = append(errs, err) if sess.logFlags&logErrors != 0 { - sess.logger.Log(ctx, msdsn.LogErrors, err.Message) + sess.logger.Log(ctx, msdsn.LogErrors, err.Message.String()) } if outs.msgq != nil { _ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgError{Error: err}) @@ -784,7 +784,7 @@ func processSingleResponse(ctx context.Context, sess *tdsSession, ch chan tokenS sess.logger.Log(ctx, msdsn.LogDebug, fmt.Sprintf("got INFO %d %s", info.Number, info.Message)) } if sess.logFlags&logMessages != 0 { - sess.logger.Log(ctx, msdsn.LogMessages, info.Message) + sess.logger.Log(ctx, msdsn.LogMessages, info.Message.String()) } if outs.msgq != nil { _ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNotice{Message: info.Message}) From 1598eaf61e43a1bb1b66231abe49f8613b544d64 Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Thu, 19 May 2022 11:22:49 -0500 Subject: [PATCH 2/2] mssql: send entire error struct as Message stringer --- error.go | 12 +++++------- go.mod | 4 ++-- go.sum | 2 ++ queries_go19_test.go | 5 +++-- token.go | 10 +++++----- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/error.go b/error.go index 58575c35..e60288a6 100644 --- a/error.go +++ b/error.go @@ -14,7 +14,7 @@ type Error struct { Number int32 State uint8 Class uint8 - Message Message + Message string ServerName string ProcName string LineNo int32 @@ -23,14 +23,12 @@ type Error struct { All []Error } -type Message string - func (e Error) Error() string { - return "mssql: " + e.Message.String() + return "mssql: " + e.Message } -func (m Message) String() string { - return string(m) +func (e Error) String() string { + return e.Message } // SQLErrorNumber returns the SQL Server error number. @@ -47,7 +45,7 @@ func (e Error) SQLErrorClass() uint8 { } func (e Error) SQLErrorMessage() string { - return e.Message.String() + return e.Message } func (e Error) SQLErrorServerName() string { diff --git a/go.mod b/go.mod index 792e0382..13c4d25b 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,11 @@ module github.com/denisenkom/go-mssqldb -go 1.11 +go 1.13 require ( github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0 github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0 github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe - github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 + github.com/golang-sql/sqlexp v0.1.0 golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 ) diff --git a/go.sum b/go.sum index f8a505f6..f4f14f65 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 h1:+eHOFJl1BaXrQxKX+T06f78590z4qA2ZzBTqahsKSE4= github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188/go.mod h1:vXjM/+wXQnTPR4KqTKDgJukSZ6amVRtWMPEjE6sQoK8= +github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A= +github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI= github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8= github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98= github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA= diff --git a/queries_go19_test.go b/queries_go19_test.go index 0534dce7..c1fabeb2 100644 --- a/queries_go19_test.go +++ b/queries_go19_test.go @@ -1,3 +1,4 @@ +//go:build go1.9 // +build go1.9 package mssql @@ -1125,10 +1126,10 @@ func TestMessageQueue(t *testing.T) { active := true msgs := []interface{}{ - sqlexp.MsgNotice{Message: Message("msg1")}, + sqlexp.MsgNotice{Message: Error{Message: "msg1"}}, sqlexp.MsgNext{}, sqlexp.MsgRowsAffected{Count: 1}, - sqlexp.MsgNotice{Message: Message("msg2")}, + sqlexp.MsgNotice{Message: Error{Message: "msg2"}}, sqlexp.MsgNextResultSet{}, } i := 0 diff --git a/token.go b/token.go index 939e61b5..61c88fcb 100644 --- a/token.go +++ b/token.go @@ -601,7 +601,7 @@ func parseError72(r *tdsBuffer) (res Error) { res.Number = r.int32() res.State = r.byte() res.Class = r.byte() - res.Message = Message(r.UsVarChar()) + res.Message = r.UsVarChar() res.ServerName = r.BVarChar() res.ProcName = r.BVarChar() res.LineNo = r.int32() @@ -615,7 +615,7 @@ func parseInfo(r *tdsBuffer) (res Error) { res.Number = r.int32() res.State = r.byte() res.Class = r.byte() - res.Message = Message(r.UsVarChar()) + res.Message = r.UsVarChar() res.ServerName = r.BVarChar() res.ProcName = r.BVarChar() res.LineNo = r.int32() @@ -773,7 +773,7 @@ func processSingleResponse(ctx context.Context, sess *tdsSession, ch chan tokenS } errs = append(errs, err) if sess.logFlags&logErrors != 0 { - sess.logger.Log(ctx, msdsn.LogErrors, err.Message.String()) + sess.logger.Log(ctx, msdsn.LogErrors, err.Message) } if outs.msgq != nil { _ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgError{Error: err}) @@ -784,10 +784,10 @@ func processSingleResponse(ctx context.Context, sess *tdsSession, ch chan tokenS sess.logger.Log(ctx, msdsn.LogDebug, fmt.Sprintf("got INFO %d %s", info.Number, info.Message)) } if sess.logFlags&logMessages != 0 { - sess.logger.Log(ctx, msdsn.LogMessages, info.Message.String()) + sess.logger.Log(ctx, msdsn.LogMessages, info.Message) } if outs.msgq != nil { - _ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNotice{Message: info.Message}) + _ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNotice{Message: info}) } case tokenReturnValue: nv := parseReturnValue(sess.buf)