feat: implement -R and -f flags for regional settings and codepage#628
feat: implement -R and -f flags for regional settings and codepage#628dlevy-msft-sql wants to merge 2 commits intomicrosoft:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements locale-aware formatting for query results when -R is specified and introduces configurable code page handling for input/output, plus associated CLI flags and documentation updates.
Changes:
- Add
RegionalSettingswith platform-specific locale detection (detectUserLocale) and integrate it into the default formatter so that-Rcontrols locale-aware numeric and date/time formatting. - Introduce code page parsing and encoding support (
ParseCodePage,GetEncoding,SupportedCodePages) and wire it into file input (:R), output (:OUT,:ERROR), and new CLI flags-f/--code-pageand--list-codepages. - Extend tests to cover regional formatting helpers, formatter construction, code page parsing/encoding, CLI argument parsing/validation, and document new
-Rand-fbehaviors inREADME.md.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
pkg/sqlcmd/sqlcmd.go |
Adds CodePage *CodePageSettings to Sqlcmd and updates IncludeFile to honor configured input code pages or BOM-based UTF-16 auto-detection when reading :R files. |
pkg/sqlcmd/regional.go |
Implements RegionalSettings (locale detection via detectUserLocale), number/money/date/time formatting, and locale-specific separators and date/time layouts. |
pkg/sqlcmd/regional_windows.go |
Windows-only locale detection using GetUserDefaultLCID and a mapping from LCID values to BCP 47 language tags. |
pkg/sqlcmd/regional_linux.go |
Linux-only locale detection from LC_ALL, LC_MESSAGES, and LANG, plus Unix locale string parsing to BCP 47 tags. |
pkg/sqlcmd/regional_darwin.go |
macOS-only locale detection using environment variables or defaults read -g AppleLocale, with Unix locale parsing similar to Linux. |
pkg/sqlcmd/regional_test.go |
Unit tests for RegionalSettings enable/disable behavior, NULL/empty handling, separators, date/time format selection, helper functions, and basic formatter construction with/without regional settings. |
pkg/sqlcmd/format.go |
Extends sqlCmdFormatterType with a regional *RegionalSettings field, adds NewSQLCmdDefaultFormatterWithRegional, and applies regional formatting to numeric and date/time columns in scanRow when -R is enabled. |
pkg/sqlcmd/commands.go |
Updates :OUT and :ERROR commands to write using either UTF-16 (for -u) or a configured output code page via GetEncoding, falling back to raw UTF-8 when appropriate. |
pkg/sqlcmd/codepage.go |
Adds CodePageSettings, ParseCodePage for -f syntax, GetEncoding for many Windows and related code pages, and SupportedCodePages metadata for listing. |
pkg/sqlcmd/codepage_test.go |
Tests ParseCodePage (including error cases and specific code pages) and GetEncoding for successful encodings and error handling for unsupported code pages. |
cmd/sqlcmd/sqlcmd.go |
Extends SQLCmdArguments with CodePage, ListCodePages, and UseRegionalSettings, validates -f, adds --code-page, --list-codepages, and -R flag wiring, lists supported code pages when requested, parses code page settings before running, and uses NewSQLCmdDefaultFormatterWithRegional to honor -R. |
cmd/sqlcmd/sqlcmd_test.go |
Adds CLI argument parsing tests for -f variations and --list-codepages, plus invalid -f cases that exercise Validate; reuses existing test harness for command-line normalization and error formatting. |
README.md |
Updates the description of -R to reflect new locale-aware formatting behavior and documents the new -f code page option and --list-codepages helper, including examples of supported code pages. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- 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
| // NewRegionalSettings creates a new RegionalSettings instance | ||
| // If enabled is false, all format methods return values unchanged | ||
| func NewRegionalSettings(enabled bool) *RegionalSettings { | ||
| r := &RegionalSettings{enabled: enabled} | ||
| if enabled { | ||
| r.tag = detectUserLocale() | ||
| r.printer = message.NewPrinter(r.tag) | ||
| r.dateFmt, r.timeFmt = getLocaleDateTimeFormats(r.tag) | ||
| } |
There was a problem hiding this comment.
NewRegionalSettings calls detectUserLocale, but this function is only implemented in regional_windows.go, regional_linux.go, and regional_darwin.go using GOOS-specific build tags. On other platforms (for example freebsd or openbsd), this package will fail to compile because detectUserLocale has no definition. Consider adding a fallback implementation in a regional_other.go file with a complementary build tag (e.g. //go:build !windows && !linux && !darwin) that returns a sensible default like language.English so cross-compilation and tests on non-main platforms continue to work.
e9d68ff to
9c9383d
Compare
-R: Locale-aware formatting for numbers, dates, times - Detects locale from Windows LCID or Unix LC_* environment variables - Applies regional thousand separators and date/time formats -f: Input/output codepage control - Format: codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage] - Use 65001 for UTF-8 - --list-codepages shows all supported encodings
9c9383d to
0c4d5dc
Compare
Summary
Implements the
-Rflag for locale-aware formatting and-fflag for codepage/encoding control, matching ODBC sqlcmd behavior.Changes
-R(Regional Settings)-f(Code Page)codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage]65001for UTF-8--list-codepagesshows all supported encodingsUsage