Skip to content

Commit 50dd816

Browse files
authoredMay 23, 2022
exit on state 127 (#88)
* exit on state 127 * call Close on liner on exit * update kong for windows file paths
1 parent e397291 commit 50dd816

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed
 

‎cmd/sqlcmd/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
211211
var line sqlcmd.Console = nil
212212
if iactive {
213213
line = console.NewConsole("")
214+
defer line.Close()
214215
}
215216

216217
s := sqlcmd.New(line, wd, vars)

‎cmd/sqlcmd/testdata/select,100.sql

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select 100

‎go.mod

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ module github.com/microsoft/go-sqlcmd
33
go 1.16
44

55
require (
6-
github.com/alecthomas/kong v0.5.0
7-
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188
6+
github.com/alecthomas/kong v0.5.1-0.20220516223738-0aaa4c11997b
7+
github.com/golang-sql/sqlexp v0.1.0
88
github.com/google/uuid v1.3.0
9-
github.com/microsoft/go-mssqldb v0.13.2
9+
github.com/microsoft/go-mssqldb v0.14.0
1010
github.com/peterh/liner v1.2.2
11+
github.com/pkg/errors v0.9.1 // indirect
1112
github.com/stretchr/testify v1.7.1
1213
golang.org/x/text v0.3.6
1314
)

‎go.sum

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0 h1:v9p9TfTbf7AwNb5NYQt7hI4
66
github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0/go.mod h1:yqy467j36fJxcRV2TzfVZ1pCb5vxm4BtZPUdYWe/Xo8=
77
github.com/alecthomas/kong v0.5.0 h1:u8Kdw+eeml93qtMZ04iei0CFYve/WPcA5IFh+9wSskE=
88
github.com/alecthomas/kong v0.5.0/go.mod h1:uzxf/HUh0tj43x1AyJROl3JT7SgsZ5m+icOv1csRhc0=
9+
github.com/alecthomas/kong v0.5.1-0.20220516223738-0aaa4c11997b h1:QF7Hdi3ReQRAST66vU7bqkHODmcVJIUZyTGo9gLHluk=
10+
github.com/alecthomas/kong v0.5.1-0.20220516223738-0aaa4c11997b/go.mod h1:GaAkr/DV/nSKftP7snQLewFh9pZqrm+OEn3HqkvWU7c=
911
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
1012
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
1113
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -14,14 +16,17 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1416
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
1517
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
1618
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
17-
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 h1:+eHOFJl1BaXrQxKX+T06f78590z4qA2ZzBTqahsKSE4=
1819
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188/go.mod h1:vXjM/+wXQnTPR4KqTKDgJukSZ6amVRtWMPEjE6sQoK8=
20+
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
21+
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
1922
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
2023
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2124
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
2225
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
2326
github.com/microsoft/go-mssqldb v0.13.2 h1:PeUn6Fu7okbaPpcv5QOk+9h7PcetOuB5ff6tVwz5V24=
2427
github.com/microsoft/go-mssqldb v0.13.2/go.mod h1:JWsC4ToRBruAAoknz9wV9Iiqb/K85J4zabR6PnBNbro=
28+
github.com/microsoft/go-mssqldb v0.14.0 h1:eo0rUzFOIImkrJi50KGpXKJe6WbrqzDqmG/yY1+yCHk=
29+
github.com/microsoft/go-mssqldb v0.14.0/go.mod h1:ti85dZ/KY16b1MsdsSm+P+04lz83uPDNdc7mBvKgwU8=
2530
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
2631
github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw=
2732
github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiNRNwI=

‎pkg/sqlcmd/sqlcmd.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type Console interface {
4444
ReadPassword(prompt string) ([]byte, error)
4545
// SetPrompt sets the prompt text shown to input the next line
4646
SetPrompt(s string)
47+
// Close clears any buffers and closes open file handles
48+
Close()
4749
}
4850

4951
// Sqlcmd is the core processor for text lines.
@@ -389,8 +391,12 @@ func (s *Sqlcmd) runQuery(query string) (int, error) {
389391
msg := retmsg.Message(ctx)
390392
switch m := msg.(type) {
391393
case sqlexp.MsgNotice:
392-
if !s.PrintError(m.Message, 10) {
393-
s.Format.AddMessage(m.Message)
394+
if !s.PrintError(m.Message.String(), 10) {
395+
s.Format.AddMessage(m.Message.String())
396+
switch e := m.Message.(type) {
397+
case mssql.Error:
398+
qe = s.handleError(&retcode, e)
399+
}
394400
}
395401
case sqlexp.MsgError:
396402
switch e := m.Error.(type) {
@@ -465,11 +471,20 @@ func (s *Sqlcmd) handleError(retcode *int, err error) error {
465471
minSeverityToExit = s.Connect.ErrorSeverityLevel
466472
}
467473
var errSeverity uint8
474+
var errState uint8
475+
var errNumber int32
468476
switch sqlError := err.(type) {
469477
case mssql.Error:
470478
errSeverity = sqlError.Class
479+
errState = sqlError.State
480+
errNumber = sqlError.Number
471481
}
472482

483+
// 127 is the magic exit code
484+
if errState == 127 {
485+
*retcode = int(errNumber)
486+
return ErrExitRequested
487+
}
473488
if s.Connect.ErrorSeverityLevel > 0 {
474489
if errSeverity >= minSeverityToExit {
475490
*retcode = int(errSeverity)

‎pkg/sqlcmd/sqlcmd_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ func TestExitCodeSetOnError(t *testing.T) {
237237
retcode, err = s.runQuery("RAISERROR (N'Testing!' , 5, 1)")
238238
assert.NoError(t, err, "ExitOnError and ErrorSeverityLevel = 0, Raiserror below 10")
239239
assert.Equal(t, -101, retcode, "ExitOnError and ErrorSeverityLevel = 0, Raiserror below 10")
240+
retcode, err = s.runQuery("RAISERROR (15001, 10, 127)")
241+
assert.ErrorIs(t, err, ErrExitRequested, "RAISERROR with state 127")
242+
assert.Equal(t, 15001, retcode, "RAISERROR (15001, 10, 127)")
240243
}
241244

242245
func TestSqlCmdExitOnError(t *testing.T) {
@@ -290,6 +293,10 @@ func (tc *testConsole) SetPrompt(s string) {
290293
tc.PromptText = s
291294
}
292295

296+
func (tc *testConsole) Close() {
297+
298+
}
299+
293300
func TestPromptForPasswordNegative(t *testing.T) {
294301
prompted := false
295302
console := &testConsole{

0 commit comments

Comments
 (0)
Please sign in to comment.