-
Notifications
You must be signed in to change notification settings - Fork 83
feat: add -f codepage flag for input/output encoding #638
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
base: main
Are you sure you want to change the base?
Conversation
|
Supersedes #627 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.
| 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if args.ListCodePages { | ||
| fmt.Println(localizer.Sprintf("Supported Code Pages:")) | ||
| fmt.Println() | ||
| fmt.Printf("%-8s %-20s %s\n", "Code", "Name", "Description") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| os.Exit(0) | ||
| } | ||
| if len(argss) > 0 { | ||
| fmt.Printf("%s'%s': Unknown command. Enter '--help' for command help.", sqlcmdErrorPrefix, argss[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shueybubbles
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
![]()
17fc3b5 to
623f029
Compare
65939c4 to
41133ed
Compare
41133ed to
0a65f11
Compare
0a65f11 to
cf7da77
Compare
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.