Skip to content

Fix piping query without go command into sqlcmd-go #583

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

Merged
merged 5 commits into from
Jun 2, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented May 25, 2025

Issue

When piping a SQL query to sqlcmd-go without a GO statement, the query was read but not executed, returning no output. However, the ODBC version of sqlcmd does execute these queries.

For example:

# Returns nothing in sqlcmd-go (before fix)
$ cat query.sql | sqlcmd-go

# Works correctly in ODBC version
$ cat query.sql | sqlcmd-odbc
master
tempdb
model
msdb
...

Root Cause

The issue was in the run function in cmd/sqlcmd/sqlcmd.go. When input is coming from stdin in non-interactive mode, the processAll parameter was always set to false. This meant that when EOF was reached, if there was no explicit GO statement, the query would not be executed.

Solution

The fix detects when stdin is a pipe (not an interactive terminal) and sets processAll=true in that case, which executes the query when EOF is reached:

iactive := args.InputFile == nil && args.Query == ""
if iactive || s.Query != "" {
    processAll := false
    if iactive {
        // Check if stdin is a pipe rather than a terminal
        fi, _ := os.Stdin.Stat()
        if fi != nil && (fi.Mode()&os.ModeCharDevice) == 0 {
            // Stdin is not a terminal, it's being piped in
            processAll = true
        }
    }
    err = s.Run(once, processAll)
}

This minimal change ensures that sqlcmd-go behaves like the ODBC version when receiving piped input, executing queries without requiring a GO statement.

Fixes #581.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • someserver
    • Triggering command: /tmp/go-build689818603/b001/sqlcmd.test -test.testlogfile=/tmp/go-build689818603/b001/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Piping query without go command into sqlcmd-go returns nothing Fix piping query without go command into sqlcmd-go May 25, 2025
@Copilot Copilot AI requested a review from shueybubbles May 25, 2025 02:40
Copilot finished work on behalf of shueybubbles May 25, 2025 02:40
Copy link
Contributor

@apoorvdeshmukh apoorvdeshmukh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@shueybubbles
Copy link
Collaborator

Instead of adding the stdin detection code in the run func, let's change isConsoleInitializationRequired to return 2 values - one for whether the app needs a console and one for whether the program is in interactive mode. If the console is only needed to get the password, then the interactive mode value will be false.

Copy link
Contributor Author

Copilot AI commented May 28, 2025

Instead of adding the stdin detection code in the run func, let's change isConsoleInitializationRequired to return 2 values - one for whether the app needs a console and one for whether the program ...

Implemented the suggested change. I've updated isConsoleInitializationRequired to return two boolean values: one for whether a console is needed and one for whether the program is in interactive mode. The function now properly determines both requirements separately, and the run function uses the interactive mode value to set processAll. This ensures that piped input without GO statements will be executed properly. (0fb01c4)

Copilot finished work on behalf of shueybubbles May 28, 2025 14:25
@shueybubbles shueybubbles marked this pull request as ready for review May 28, 2025 15:00
@shueybubbles shueybubbles merged commit e8f9c26 into main Jun 2, 2025
7 checks passed
@shueybubbles shueybubbles deleted the copilot/fix-581 branch June 2, 2025 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Piping query without go command into sqlcmd-go returns nothing
4 participants