From 7cadf91a8291f02403121a897f049b4cb5314ef8 Mon Sep 17 00:00:00 2001 From: Gabriel Corado Date: Mon, 10 Jul 2023 16:32:42 -0300 Subject: [PATCH] feat(dbcmd): add `sqlcmd` support --- lib/client/db/dbcmd/dbcmd.go | 14 ++++++++++++++ lib/client/db/dbcmd/dbcmd_test.go | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/client/db/dbcmd/dbcmd.go b/lib/client/db/dbcmd/dbcmd.go index 83cbb73ca7a9..7812a1afb172 100644 --- a/lib/client/db/dbcmd/dbcmd.go +++ b/lib/client/db/dbcmd/dbcmd.go @@ -58,6 +58,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. @@ -408,6 +410,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. @@ -533,6 +541,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. @@ -547,6 +557,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...) } diff --git a/lib/client/db/dbcmd/dbcmd_test.go b/lib/client/db/dbcmd/dbcmd_test.go index 251697e31af5..ae7b10c94b1a 100644 --- a/lib/client/db/dbcmd/dbcmd_test.go +++ b/lib/client/db/dbcmd/dbcmd_test.go @@ -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,