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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@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
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
2 participants