From 9920d8a13d5bd07016a1c6223be841505ec521b4 Mon Sep 17 00:00:00 2001 From: davidshi Date: Wed, 11 Dec 2024 17:47:21 -0600 Subject: [PATCH 1/3] print headers when there are 0 rows --- pkg/sqlcmd/sqlcmd.go | 22 +++++++++++----------- pkg/sqlcmd/sqlcmd_test.go | 11 +++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/pkg/sqlcmd/sqlcmd.go b/pkg/sqlcmd/sqlcmd.go index bf4f30b0..f4c68224 100644 --- a/pkg/sqlcmd/sqlcmd.go +++ b/pkg/sqlcmd/sqlcmd.go @@ -474,19 +474,19 @@ func (s *Sqlcmd) runQuery(query string) (int, error) { first = true } case sqlexp.MsgNext: + if first { + first = false + cols, err = rows.ColumnTypes() + if err != nil { + retcode = -100 + qe = s.handleError(&retcode, err) + s.Format.AddError(err) + } else { + s.Format.BeginResultSet(cols) + } + } inresult := rows.Next() for inresult { - if first { - first = false - cols, err = rows.ColumnTypes() - if err != nil { - retcode = -100 - qe = s.handleError(&retcode, err) - s.Format.AddError(err) - } else { - s.Format.BeginResultSet(cols) - } - } col1 := s.Format.AddRow(rows) inresult = rows.Next() if !inresult { diff --git a/pkg/sqlcmd/sqlcmd_test.go b/pkg/sqlcmd/sqlcmd_test.go index 36f0d5dc..7016532a 100644 --- a/pkg/sqlcmd/sqlcmd_test.go +++ b/pkg/sqlcmd/sqlcmd_test.go @@ -521,6 +521,17 @@ func TestQueryServerPropertyReturnsColumnName(t *testing.T) { } } +func TestHeadersPrintWhenThereAreZeroRows(t *testing.T) { + s, buf := setupSqlCmdWithMemoryOutput(t) + s.vars.Set(SQLCMDMAXVARTYPEWIDTH, "256") + s.vars.Set(SQLCMDCOLWIDTH, "4") + defer buf.Close() + err := runSqlCmd(t, s, []string{"select name from sys.databases where name like 'bogus'", "GO"}) + if assert.NoError(t, err, "select should succeed") { + assert.Contains(t, buf.buf.String(), "name"+SqlcmdEol+"----"+SqlcmdEol, "Headers missing from output") + } +} + func TestSqlCmdOutputAndError(t *testing.T) { s, outfile, errfile := setupSqlcmdWithFileErrorOutput(t) defer os.Remove(outfile.Name()) From ce20ec46437d945f450800c41e5b059390cbad84 Mon Sep 17 00:00:00 2001 From: davidshi Date: Wed, 11 Dec 2024 19:17:17 -0600 Subject: [PATCH 2/3] update driver to 1.8 --- go.mod | 4 ++-- go.sum | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 217e6b7c..e9a5118f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/microsoft/go-sqlcmd -go 1.21 +go 1.22 require ( github.com/alecthomas/chroma/v2 v2.5.0 @@ -10,7 +10,7 @@ require ( github.com/docker/go-connections v0.4.0 github.com/golang-sql/sqlexp v0.1.0 github.com/google/uuid v1.6.0 - github.com/microsoft/go-mssqldb v1.7.2 + github.com/microsoft/go-mssqldb v1.8.0 github.com/opencontainers/image-spec v1.0.2 github.com/peterh/liner v1.2.2 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index df49dbc9..76c41caf 100644 --- a/go.sum +++ b/go.sum @@ -251,6 +251,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0j github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/microsoft/go-mssqldb v1.7.2 h1:CHkFJiObW7ItKTJfHo1QX7QBBD1iV+mn1eOyRP3b/PA= github.com/microsoft/go-mssqldb v1.7.2/go.mod h1:kOvZKUdrhhFQmxLZqbwUV0rHkNkZpthMITIb2Ko1IoA= +github.com/microsoft/go-mssqldb v1.8.0 h1:7cyZ/AT7ycDsEoWPIXibd+aVKFtteUNhDGf3aobP+tw= +github.com/microsoft/go-mssqldb v1.8.0/go.mod h1:6znkekS3T2vp0waiMhen4GPU1BiAsrP+iXHcE7a7rFo= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/moby/term v0.0.0-20221120202655-abb19827d345 h1:J9c53/kxIH+2nTKBEfZYFMlhghtHpIHSXpm5VRGHSnU= From 05dcf48d7606a2325fe75051eff0e0b3a1d79159 Mon Sep 17 00:00:00 2001 From: davidshi Date: Wed, 11 Dec 2024 19:23:00 -0600 Subject: [PATCH 3/3] update go in workflows --- .github/workflows/pr-validation.yml | 2 +- .pipelines/include-install-go-tools.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pr-validation.yml b/.github/workflows/pr-validation.yml index 725afc10..ad21c30d 100644 --- a/.github/workflows/pr-validation.yml +++ b/.github/workflows/pr-validation.yml @@ -13,7 +13,7 @@ jobs: - name: Setup go uses: actions/setup-go@v2 with: - go-version: '1.21' + go-version: '1.22' - name: Run tests against Linux SQL run: | go version diff --git a/.pipelines/include-install-go-tools.yml b/.pipelines/include-install-go-tools.yml index 23d7190d..bcd7053b 100644 --- a/.pipelines/include-install-go-tools.yml +++ b/.pipelines/include-install-go-tools.yml @@ -1,7 +1,7 @@ steps: - task: GoTool@0 inputs: - version: '1.18' + version: '1.22' - task: Go@0 displayName: 'Go: get dependencies' inputs: