Skip to content

feat: implement :perftrace and :help commands#632

Open
dlevy-msft-sql wants to merge 5 commits intomicrosoft:mainfrom
dlevy-msft-sql:perftrace
Open

feat: implement :perftrace and :help commands#632
dlevy-msft-sql wants to merge 5 commits intomicrosoft:mainfrom
dlevy-msft-sql:perftrace

Conversation

@dlevy-msft-sql
Copy link
Copy Markdown
Contributor

@dlevy-msft-sql dlevy-msft-sql commented Jan 25, 2026

Summary

Adds two new interactive commands: :help and :perftrace.

:help displays a reference of all available sqlcmd commands with short descriptions. It can also show help for a single command: :help connect.

:perftrace redirects performance statistics output (from the -p flag, PR #631) to a file, stderr, or stdout -- the same way :out and :error redirect query and error output.

New commands

Command Description
:help Lists all available commands with descriptions
:help <command> Shows help for a single command (case-insensitive)
:perftrace <file> Redirects timing output to a file, stderr, or stdout

Usage

1> :help
:!! [<command>]
  - Executes a command in the operating system shell.
:connect server[\instance] [-l timeout] [-U user [-P password]]
  - Connects to a SQL Server instance.
...

1> :help connect
:connect server[\instance] [-l timeout] [-U user [-P password]]
  - Connects to a SQL Server instance.

1> :perftrace c:/logs/perf.txt
1> select 1
2> go

Implementation

  • Added a help field to the Command struct. Every registered command carries its own help text, so :help output is always in sync with the command registry. A TestAllCommandsHaveHelp test guards against drift.
  • :help <command> does a case-insensitive lookup into the command map. Unknown names fall through to the full listing.
  • Extracted redirectWriter() to share file/stderr/stdout resolution logic across :out, :error, and :perftrace (was duplicated between outCommand and errorCommand).
  • Added stat (io.WriteCloser) field to Sqlcmd with GetStat()/SetStat() accessors. GetStat() falls back to GetOutput() when no :perftrace redirection is active, so existing -p output is unaffected.
  • SetStat(nil) called during cleanup in cmd/sqlcmd/sqlcmd.go to close any open perf trace file.

Testing

  • TestHelpCommand: full listing, per-command filtering, case-insensitive lookup, unknown command fallback
  • TestAllCommandsHaveHelp: drift guard ensuring every command has help text
  • TestPerftraceCommand: empty arg error, stdout, stderr, file redirect, variable resolution
  • TestCommandParsing: parsing for :help, :help CONNECT, :perftrace stderr, etc.
  • All tests run without a database connection

Merge notes

Fixes #622.

Copy link
Copy Markdown

Copilot AI left a 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 implements the :perftrace command to redirect performance statistics output to a file, stderr, or stdout, working in conjunction with the -p flag from PR #631.

Changes:

  • Added stat writer infrastructure with GetStat/SetStat methods following the established pattern for output/error writers
  • Implemented PERFTRACE command supporting file paths, "stdout", and "stderr" with variable substitution
  • Added comprehensive test coverage for the command functionality

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
pkg/sqlcmd/sqlcmd.go Added stat field and GetStat()/SetStat() methods for managing performance statistics output writer
pkg/sqlcmd/commands.go Registered PERFTRACE command and implemented perftraceCommand() function with file/stdout/stderr support
pkg/sqlcmd/commands_test.go Added TestPerftraceCommand() with comprehensive test scenarios and added PERFTRACE parsing tests
README.md Documented the :perftrace command with usage example

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a 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 4 out of 4 changed files in this pull request and generated 6 comments.

Comment thread pkg/sqlcmd/commands_test.go
Comment thread pkg/sqlcmd/commands_test.go Outdated
Comment thread pkg/sqlcmd/commands.go Outdated
Comment thread README.md Outdated
Comment thread pkg/sqlcmd/commands.go Outdated
Comment thread pkg/sqlcmd/commands_test.go
Copy link
Copy Markdown

Copilot AI left a 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 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread README.md Outdated
Copy link
Copy Markdown

Copilot AI left a 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 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread pkg/sqlcmd/sqlcmd.go
Copy link
Copy Markdown

Copilot AI left a 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 5 out of 5 changed files in this pull request and generated no new comments.

@dlevy-msft-sql dlevy-msft-sql self-assigned this Jan 25, 2026
@dlevy-msft-sql dlevy-msft-sql added sqlcmd switch switch in existing sqlcmd Size: S Small issue (less than one week effort) labels Jan 25, 2026
@dlevy-msft-sql dlevy-msft-sql changed the title Implement :perftrace command Implement :perftrace and :help commands Feb 5, 2026
@dlevy-msft-sql dlevy-msft-sql force-pushed the perftrace branch 2 times, most recently from e7abfe7 to 4d25bf2 Compare February 5, 2026 19:58
@dlevy-msft-sql dlevy-msft-sql changed the title Implement :perftrace and :help commands feat: implement :perftrace and :help commands Feb 5, 2026
:perftrace redirects timing/statistics output to a file, stderr, or stdout.
:help displays the list of available sqlcmd commands with usage.

Both commands validate arguments and return appropriate errors.
dlevy-msft-sql added a commit to dlevy-msft-sql/go-sqlcmd that referenced this pull request Apr 17, 2026
Accept an io.Writer parameter instead of hardcoding s.GetOutput(),
enabling PR microsoft#632 (:perftrace) to redirect statistics output via
s.GetStat() after merge.
After merging print-statistics, the printStatistics call site should
pass s.GetStat() instead of s.GetOutput() so :perftrace redirection
applies to performance statistics output.
- Generate :help output from registered Command.help fields instead of
  hardcoded string literal (eliminates drift when commands are added)
- Add help field to every registered command in newCommands()
- Extract redirectWriter() to deduplicate outCommand/errorCommand/perftraceCommand
- Remove DB dependency from TestHelpCommand and TestPerftraceCommand
- Add TestAllCommandsHaveHelp to catch commands with missing help text
- Strengthen TestHelpCommand to cross-reference registered help fields
- Remove stale PR merge note from GetStat() godoc
- :HELP <command> now shows help for a single command (case-insensitive)
- Unknown command names fall through to the full help listing
- Sort help entries by help text (stable: all start with command name)
- README: clarify :perftrace requires -p flag for timing data
- Add tests for per-command help, case-insensitive lookup, and
  unknown command fallback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Size: S Small issue (less than one week effort) sqlcmd switch switch in existing sqlcmd

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: :TIMING ON/OFF command for interactive sessions

2 participants