feat: add -f codepage flag for input/output encoding#638
Open
dlevy-msft-sql wants to merge 1 commit intomicrosoft:mainfrom
Open
feat: add -f codepage flag for input/output encoding#638dlevy-msft-sql wants to merge 1 commit intomicrosoft:mainfrom
dlevy-msft-sql wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Contributor
Author
|
Supersedes #627 |
There was a problem hiding this comment.
Pull request overview
This PR adds configurable input/output code page handling to the legacy sqlcmd CLI, improving compatibility with ODBC sqlcmd and enabling legacy-encoding workflows.
Changes:
- Introduces a central code page registry and parsing logic (
-f/--code-page) with support for many Windows, OEM, ISO-8859, CJK, and EBCDIC encodings and a--list-codepageslisting mode. - Wires code page settings through the legacy CLI into the core
Sqlcmdengine so:R,:OUT, and:ERRORcommands correctly decode input files and encode output/error files according to the configured code pages. - Adds comprehensive unit tests for code page parsing/lookup and CLI argument validation, plus README documentation and examples for the new functionality.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
pkg/sqlcmd/sqlcmd_test.go |
Adds tests to verify IncludeFile decodes input files correctly under various input code pages (Windows-1252, UTF-16LE/BE with and without BOM). |
pkg/sqlcmd/sqlcmd.go |
Extends Sqlcmd with a CodePage field and updates IncludeFile to select an appropriate io.Reader based on configured input code page or BOM auto-detection. |
pkg/sqlcmd/commands_test.go |
Adds tests confirming :OUT and :ERROR commands honor the configured output code page when writing to files. |
pkg/sqlcmd/commands.go |
Introduces a transformWriteCloser helper and updates :OUT/:ERROR commands to encode output using the selected code page while ensuring underlying files are closed correctly. |
pkg/sqlcmd/codepage_windows.go |
Implements Windows-specific encoding.Encoding support backed by MultiByteToWideChar/WideCharToMultiByte for system-installed code pages not in the built-in registry. |
pkg/sqlcmd/codepage_test.go |
Adds tests for ParseCodePage, GetEncoding, SupportedCodePages, and Windows-specific fallback behavior, including round-trip checks for a Windows-only EBCDIC code page. |
pkg/sqlcmd/codepage_other.go |
Provides a non-Windows implementation of getSystemCodePageEncoding that surfaces a clear error for unsupported system code pages. |
pkg/sqlcmd/codepage.go |
Defines the cross-platform code page registry, CodePageSettings, parsing/validation logic for -f, and a SupportedCodePages API used by the CLI. |
cmd/sqlcmd/sqlcmd_test.go |
Extends CLI argument tests to cover -f / --code-page, --list-codepages, and various invalid code page argument combinations. |
cmd/sqlcmd/sqlcmd.go |
Adds CodePage and ListCodePages arguments, validates and stores parsed CodePageSettings, applies them to the Sqlcmd instance, and implements --list-codepages output formatting. |
README.md |
Documents the new -f flag, describes supported code pages, provides usage examples, and explains --list-codepages and default BOM auto-detection behavior. |
| err = localizer.Errorf("The -J parameter requires encryption to be enabled (-N true, -N mandatory, or -N strict).") | ||
| case a.CodePage != "": | ||
| if codePageSettings, parseErr := sqlcmd.ParseCodePage(a.CodePage); parseErr != nil { | ||
| err = localizer.Errorf(`'-f %s': %v`, a.CodePage, parseErr) |
Collaborator
| if args.ListCodePages { | ||
| fmt.Println(localizer.Sprintf("Supported Code Pages:")) | ||
| fmt.Println() | ||
| fmt.Printf("%-8s %-20s %s\n", "Code", "Name", "Description") |
Collaborator
| os.Exit(0) | ||
| } | ||
| if len(argss) > 0 { | ||
| fmt.Printf("%s'%s': Unknown command. Enter '--help' for command help.", sqlcmdErrorPrefix, argss[0]) |
Collaborator
Collaborator
6 tasks
17fc3b5 to
623f029
Compare
65939c4 to
41133ed
Compare
41133ed to
0a65f11
Compare
0a65f11 to
cf7da77
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the
-fflag from ODBC sqlcmd for specifying input and output file code pages.Changes
CodePageandListCodePagesfields,-f/--code-pageflag, validation, and runtime application:Rcommand to respect codepage settings when reading filesCodePagefield to Sqlcmd structUsage
Supported Codepages
Testing
Improves ODBC sqlcmd compatibility.