feat(dbcmd): add sqlcmd support (#28918)

This commit is contained in:
Gabriel Corado 2023-07-11 09:42:59 -03:00 committed by GitHub
parent 800e79b113
commit caacc54696
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View file

@ -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.
@ -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.
@ -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.
@ -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...)
}

View file

@ -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,