Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print headers when zero rows are returned #558

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pr-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .pipelines/include-install-go-tools.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
steps:
- task: GoTool@0
inputs:
version: '1.18'
version: '1.22'
- task: Go@0
displayName: 'Go: get dependencies'
inputs:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
22 changes: 11 additions & 11 deletions pkg/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions pkg/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading