Skip to content

Fix rendering of dynamic time values #82

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

Merged
merged 2 commits into from
Apr 27, 2022
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ require (
golang.org/x/text v0.3.6
)

replace github.com/denisenkom/go-mssqldb => github.com/shueybubbles/go-mssqldb v0.10.1-0.20220317022252-fafb9d92e469
replace github.com/denisenkom/go-mssqldb => github.com/microsoft/go-mssqldb v0.12.1-0.20220421181353-0db958cd919d
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/microsoft/go-mssqldb v0.12.1-0.20220421181353-0db958cd919d h1:tsq7LhO5f4wOIPzLhDnwy2MAEQJTjc8KNu1LGmVwxJk=
github.com/microsoft/go-mssqldb v0.12.1-0.20220421181353-0db958cd919d/go.mod h1:iiK0YP1ZeepvmBQk/QpLEhhTNJgfzrpArPY/aFvc9yU=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw=
github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiNRNwI=
Expand All @@ -29,8 +31,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shueybubbles/go-mssqldb v0.10.1-0.20220317022252-fafb9d92e469 h1:BuUMqsxB86i1QEBf0q+dkQYfNLVpD1nH1fRJPKvXWSg=
github.com/shueybubbles/go-mssqldb v0.10.1-0.20220317022252-fafb9d92e469/go.mod h1:iiK0YP1ZeepvmBQk/QpLEhhTNJgfzrpArPY/aFvc9yU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
Expand All @@ -42,7 +42,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b h1:k+E048sYJHyVnsr1GDrRZWQ32D2C7lWs9JRc0bel53A=
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
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-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -55,7 +54,6 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
42 changes: 41 additions & 1 deletion pkg/sqlcmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type columnDetail struct {
leftJustify bool
zeroesAfterDecimal bool
col sql.ColumnType
precision int
scale int
}

// The default formatter based on the native sqlcmd style
Expand Down Expand Up @@ -304,6 +306,11 @@ func calcColumnDetails(cols []*sql.ColumnType, fixed int64, variable int64) ([]c
columnDetails[i].col = *c
columnDetails[i].leftJustify = true
columnDetails[i].zeroesAfterDecimal = false
p, s, ok := c.DecimalSize()
if ok {
columnDetails[i].precision = int(p)
columnDetails[i].scale = int(s)
}
if length == 0 {
columnDetails[i].displayWidth = defaultMaxDisplayWidth
} else {
Expand Down Expand Up @@ -359,6 +366,9 @@ func calcColumnDetails(cols []*sql.ColumnType, fixed int64, variable int64) ([]c
columnDetails[i].leftJustify = false
columnDetails[i].displayWidth = max64(38, nameLen)
columnDetails[i].zeroesAfterDecimal = true
case "TIME":
columnDetails[i].leftJustify = false
columnDetails[i].displayWidth = max64(16, nameLen)
case "DATETIMEOFFSET":
columnDetails[i].leftJustify = false
columnDetails[i].displayWidth = max64(45, nameLen)
Expand Down Expand Up @@ -451,7 +461,26 @@ func (f *sqlCmdFormatterType) scanRow(rows *sql.Rows) ([]string, error) {
row[n] = x
case time.Time:
// Go lacks any way to get the user's preferred time format or even the system default
row[n] = x.String()
switch f.columnDetails[n].col.DatabaseTypeName() {
case "DATE":
row[n] = x.Format("2006-01-02")
case "DATETIME":
row[n] = x.Format(dateTimeFormatString(3, false))
case "DATETIME2":
row[n] = x.Format(dateTimeFormatString(f.columnDetails[n].scale, false))
case "SMALLDATETIME":
row[n] = x.Format(dateTimeFormatString(0, false))
case "DATETIMEOFFSET":
row[n] = x.Format(dateTimeFormatString(f.columnDetails[n].scale, true))
case "TIME":
format := "15:04:05"
if f.columnDetails[n].scale > 0 {
format = fmt.Sprintf("%s.%0*d", format, f.columnDetails[n].scale, 0)
}
row[n] = x.Format(format)
default:
row[n] = x.Format(time.RFC3339)
}
case fmt.Stringer:
row[n] = x.String()
// not sure why go-mssql reports bit as bool
Expand All @@ -472,6 +501,17 @@ func (f *sqlCmdFormatterType) scanRow(rows *sql.Rows) ([]string, error) {
return row, nil
}

func dateTimeFormatString(scale int, addOffset bool) string {
format := `2006-01-02 15:04:05`
if scale > 0 {
format = fmt.Sprintf("%s.%0*d", format, scale, 0)
}
if addOffset {
format += " -07:00"
}
return format
}

// Prints the final version of a cell based on formatting variables and command line parameters
func (f *sqlCmdFormatterType) printColumnValue(val string, col int) {
c := f.columnDetails[col]
Expand Down
13 changes: 13 additions & 0 deletions pkg/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,19 @@ func TestSqlCmdMaintainsConnectionBetweenBatches(t *testing.T) {
}
}

func TestDateTimeFormats(t *testing.T) {
s, buf := setupSqlCmdWithMemoryOutput(t)
defer buf.Close()
err := s.IncludeFile(`testdata/selectdates.sql`, true)
if assert.NoError(t, err, "selectdates.sql") {
assert.Equal(t,
`2022-03-05 14:01:02.000 2021-01-02 11:06:02.2000 2021-05-05 00:00:00.000000 +00:00 2019-01-11 13:00:00 14:01:02.0000000 2011-02-03`+SqlcmdEol+SqlcmdEol,
buf.buf.String(),
"Unexpected date format output")

}
}

// runSqlCmd uses lines as input for sqlcmd instead of relying on file or console input
func runSqlCmd(t testing.TB, s *Sqlcmd, lines []string) error {
t.Helper()
Expand Down
9 changes: 9 additions & 0 deletions pkg/sqlcmd/testdata/selectdates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set nocount on
declare @d1 datetime = '2022-03-05 14:01:02'
declare @d2 datetime2(4) = '2021-1-2 11:06:02.2'
declare @d3 datetimeoffset(6) = '2021-5-5'
declare @d4 smalldatetime = '2019-01-11 13:00:00'
declare @d5 time = '14:01:02'
declare @d6 date = '2011-02-03'

select @d1, @d2, @d3, @d4, @d5, @d6