Skip to content

Commit

Permalink
feat(dbcmd): add sqlcmd support (#28918)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielcorado authored Jul 11, 2023
1 parent 800e79b commit caacc54
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/client/db/dbcmd/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const (
redisBin = "redis-cli"
// mssqlBin is the SQL Server client program name.
mssqlBin = "mssql-cli"
// sqlcmd is the SQL Server client program name.
sqlcmdBin = "sqlcmd"
// snowsqlBin is the Snowflake client program name.
snowsqlBin = "snowsql"
// cqlshBin is the Cassandra client program name.
Expand Down Expand Up @@ -429,6 +431,12 @@ func (c *CLICommandBuilder) isMySQLBinMariaDBFlavor() (bool, error) {
return strings.Contains(strings.ToLower(string(mysqlVer)), "mariadb"), nil
}

// isSqlcmdAvailable returns true if "sqlcmd" binary is fouind in the system
// PATH.
func (c *CLICommandBuilder) isSqlcmdAvailable() bool {
return c.isBinAvailable(sqlcmdBin)
}

func (c *CLICommandBuilder) shouldUseMongoshBin() bool {
// Use "mongosh" if available.
// If not, use legacy "mongo" if available.
Expand Down Expand Up @@ -554,6 +562,8 @@ func (c *CLICommandBuilder) getRedisCommand() *exec.Cmd {
return exec.Command(redisBin, args...)
}

// getSQLServerCommand returns a command to connect to SQL Server.
// mssql-cli and sqlcmd commands have the same argument names.
func (c *CLICommandBuilder) getSQLServerCommand() *exec.Cmd {
args := []string{
// Host and port must be comma-separated.
Expand All @@ -568,6 +578,10 @@ func (c *CLICommandBuilder) getSQLServerCommand() *exec.Cmd {
args = append(args, "-d", c.db.Database)
}

if c.isSqlcmdAvailable() {
return exec.Command(sqlcmdBin, args...)
}

return exec.Command(mssqlBin, args...)
}

Expand Down
17 changes: 17 additions & 0 deletions lib/client/db/dbcmd/dbcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,23 @@ func TestCLICommandBuilderGetConnectCommand(t *testing.T) {
},
wantErr: false,
},
{
name: "sqlserver sqlcmd",
dbProtocol: defaults.ProtocolSQLServer,
databaseName: "mydb",
execer: &fakeExec{
execOutput: map[string][]byte{
"sqlcmd": {},
},
},
cmd: []string{sqlcmdBin,
"-S", "localhost,12345",
"-U", "myUser",
"-P", fixtures.UUID,
"-d", "mydb",
},
wantErr: false,
},
{
name: "redis-cli",
dbProtocol: defaults.ProtocolRedis,
Expand Down

0 comments on commit caacc54

Please sign in to comment.