Skip to content

Commit a046229

Browse files
Fix review comments for PR microsoft#628
- Fix nil encoder panic in errorCommand when using UTF-8 codepage - Improve error handling with proper file close on encoding error - Remove dead code (unused 'err' variable) in format.go - Add missing -R flag test in TestValidCommandLineToArgsConversion
1 parent 448871d commit a046229

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

cmd/sqlcmd/sqlcmd_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ func TestValidCommandLineToArgsConversion(t *testing.T) {
139139
{[]string{"--list-codepages"}, func(args SQLCmdArguments) bool {
140140
return args.ListCodePages
141141
}},
142+
// Regional settings flag test
143+
{[]string{"-R"}, func(args SQLCmdArguments) bool {
144+
return args.UseRegionalSettings
145+
}},
146+
{[]string{"--client-regional-setting"}, func(args SQLCmdArguments) bool {
147+
return args.UseRegionalSettings
148+
}},
142149
}
143150

144151
for _, test := range commands {

pkg/sqlcmd/commands.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,18 @@ func errorCommand(s *Sqlcmd, args []string, line uint) error {
370370
if s.CodePage != nil && s.CodePage.OutputCodePage != 0 {
371371
enc, err := GetEncoding(s.CodePage.OutputCodePage)
372372
if err != nil {
373-
o.Close()
373+
if cerr := o.Close(); cerr != nil {
374+
return fmt.Errorf("%w (and closing error file %s failed: %v)", err, filePath, cerr)
375+
}
374376
return err
375377
}
376-
encoder := transform.NewWriter(o, enc.NewEncoder())
377-
s.SetError(encoder)
378+
if enc == nil {
379+
// No transformation required (e.g., UTF-8), write directly
380+
s.SetError(o)
381+
} else {
382+
encoder := transform.NewWriter(o, enc.NewEncoder())
383+
s.SetError(encoder)
384+
}
378385
} else {
379386
s.SetError(o)
380387
}

pkg/sqlcmd/format.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,6 @@ func (f *sqlCmdFormatterType) scanRow(rows *sql.Rows) ([]string, error) {
567567
row[n] = "0"
568568
}
569569
default:
570-
var err error
571570
val := fmt.Sprintf("%v", x)
572571
// Apply regional formatting for numeric types
573572
if f.regional.IsEnabled() {
@@ -582,9 +581,6 @@ func (f *sqlCmdFormatterType) scanRow(rows *sql.Rows) ([]string, error) {
582581
} else {
583582
row[n] = val
584583
}
585-
if err != nil {
586-
return nil, err
587-
}
588584
}
589585
}
590586
}

0 commit comments

Comments
 (0)