Skip to content

Commit

Permalink
fix: When both -Q and -q are specified, take -Q input and finish (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkunc committed Aug 14, 2024
1 parent 5f78da8 commit 412fe02
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cmd/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,11 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {

if err == nil && s.Exitcode == 0 {
once := false
if args.InitialQuery != "" {
s.Query = args.InitialQuery
} else if args.Query != "" {
if args.Query != "" {
once = true
s.Query = args.Query
} else if args.InitialQuery != "" {
s.Query = args.InitialQuery
}
iactive := args.InputFile == nil && args.Query == ""
if iactive || s.Query != "" {
Expand Down
28 changes: 28 additions & 0 deletions cmd/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,34 @@ func TestQueryAndExit(t *testing.T) {
}
}

func TestInitQueryAndQueryExecutesQuery(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Panic occurred: %v", r)
}
}()
o, err := os.CreateTemp("", "sqlcmdmain")
assert.NoError(t, err, "os.CreateTemp")
defer os.Remove(o.Name())
defer o.Close()
args = newArguments()
args.InitialQuery = "SELECT 1"
args.Query = "SELECT 2"
args.OutputFile = o.Name()
vars := sqlcmd.InitializeVariables(args.useEnvVars())
vars.Set(sqlcmd.SQLCMDMAXVARTYPEWIDTH, "0")

setVars(vars, &args)

exitCode, err := run(vars, &args)
assert.NoError(t, err, "run")
assert.Equal(t, 0, exitCode, "exitCode")
bytes, err := os.ReadFile(o.Name())
if assert.NoError(t, err, "os.ReadFile") {
assert.Equal(t, "2"+sqlcmd.SqlcmdEol+sqlcmd.SqlcmdEol+oneRowAffected+sqlcmd.SqlcmdEol, string(bytes), "Incorrect output from run")
}
}

// Test to verify fix for issue: https://github.com/microsoft/go-sqlcmd/issues/98
// 1. Verify when -b is passed in (ExitOnError), we don't always get an error (even when input is good)
// 2, Verify when the input is actually bad, we do get an error
Expand Down

0 comments on commit 412fe02

Please sign in to comment.