Skip to content

Commit 635cb07

Browse files
Fix float formatting to use DatabaseTypeName for bitSize and add connectCommand validation
- Use DatabaseTypeName() to determine bitSize (32 for REAL, 64 for FLOAT) instead of relying on Go type - Add validation in connectCommand to prevent panic when invoked without parameters - Fix import grouping in console_redirect.go to follow Go conventions Co-authored-by: dlevy-msft-sql <[email protected]>
1 parent 6a8b162 commit 635cb07

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

pkg/console/console_redirect.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
package console
55

66
import (
7-
"golang.org/x/term"
87
"os"
8+
9+
"golang.org/x/term"
910
)
1011

1112
// isStdinRedirected checks if stdin is coming from a pipe or redirection

pkg/sqlcmd/commands.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ func connectCommand(s *Sqlcmd, args []string, line uint) error {
466466
}
467467

468468
commandArgs := strings.Fields(args[0])
469+
470+
// Require at least the server name parameter
471+
if len(commandArgs) == 0 {
472+
return InvalidCommandError("CONNECT", line)
473+
}
469474

470475
// Parse flags
471476
flags := flag.NewFlagSet("connect", flag.ContinueOnError)

pkg/sqlcmd/format.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,17 +535,24 @@ func (f *sqlCmdFormatterType) scanRow(rows *sql.Rows) ([]string, error) {
535535
// Format float64 to match ODBC sqlcmd behavior
536536
// Use 'f' format with -1 precision to avoid scientific notation for typical values
537537
// Fall back to 'g' format if the result would exceed the column display width
538-
formatted := strconv.FormatFloat(x, 'f', -1, 64)
538+
539+
// Use appropriate bitSize based on the SQL type (REAL=32, FLOAT=64)
540+
bitSize := 64
541+
if f.columnDetails[n].col.DatabaseTypeName() == "REAL" {
542+
bitSize = 32
543+
}
544+
545+
formatted := strconv.FormatFloat(x, 'f', -1, bitSize)
539546
displayWidth := f.columnDetails[n].displayWidth
540547
if displayWidth > 0 && int64(len(formatted)) > displayWidth {
541548
// Use 'g' format for very large/small values to avoid truncation issues
542-
formatted = strconv.FormatFloat(x, 'g', -1, 64)
549+
formatted = strconv.FormatFloat(x, 'g', -1, bitSize)
543550
}
544551
row[n] = formatted
545552
case float32:
546553
// Format float32 to match ODBC sqlcmd behavior
547-
// Use 'f' format with -1 precision to avoid scientific notation for typical values
548-
// Fall back to 'g' format if the result would exceed the column display width
554+
// float32 values are rare (database/sql typically normalizes to float64)
555+
// but handle them if they occur
549556
formatted := strconv.FormatFloat(float64(x), 'f', -1, 32)
550557
displayWidth := f.columnDetails[n].displayWidth
551558
if displayWidth > 0 && int64(len(formatted)) > displayWidth {

0 commit comments

Comments
 (0)