This commit is contained in:
tariqibrahim 2018-11-01 21:55:53 -07:00
Родитель f6d661886a 2dc72b96b8
Коммит c510825bac
4 изменённых файлов: 74 добавлений и 9 удалений

Просмотреть файл

@ -121,7 +121,7 @@ func main() {
schemas := text.SplitNonEmpty(*schemasList, ",")
if err := logic.QueryHosts(hosts, schemas, queries, *user, *password, *defaultSchema, *maxConcurrency, *timeout); err != nil {
if err := logic.QueryHosts(hosts, *user, *password, *defaultSchema, schemas, queries, *maxConcurrency, *timeout); err != nil {
os.Exit(1)
}
}

Просмотреть файл

@ -11,11 +11,7 @@ import (
// queryHost connects to a given host, issues the given set of queries, and outputs the results
// line per row in tab delimited format
func queryHost(
host, user, password, schema string,
queries []string,
timeout float64, printSchema bool,
) error {
func queryHost(host string, user string, password string, schema string, queries []string, timeout float64, printSchema bool) error {
mysqlURI := fmt.Sprintf("%s:%s@tcp(%s)/%s?timeout=%fs", user, password, host, schema, timeout)
db, _, err := sqlutils.GetDB(mysqlURI)
if err != nil {
@ -42,9 +38,8 @@ func queryHost(
}
// QueryHosts will issue concurrent queries on given list of hosts
func QueryHosts(
hosts, schemas, queries []string,
user, password, defaultSchema string,
func QueryHosts(hosts []string, user string, password string,
defaultSchema string, schemas []string, queries []string,
maxConcurrency uint, timeout float64,
) (anyError error) {
concurrentQueries := make(chan bool, maxConcurrency)

Просмотреть файл

@ -1,6 +1,8 @@
package sql
import (
"io/ioutil"
"os"
"strings"
"testing"
)
@ -82,3 +84,32 @@ func TestParseQueriesQuotes(t *testing.T) {
t.Errorf("got unexpected results: `%+v`", result)
}
}
func TestParseQueriesFile(t *testing.T) {
queriesText := `select 1; select '2;2' ; select 3`
tmpFile, err := ioutil.TempFile(os.TempDir(), "querytest-")
if err != nil {
t.Errorf("error creating temporary file: %s", err.Error())
}
defer os.Remove(tmpFile.Name())
if _, err = tmpFile.Write([]byte(queriesText)); err != nil {
t.Errorf("error while trying to write to temporary file %s: %s", tmpFile.Name(), err.Error())
}
queries, err := ParseQueries("", tmpFile.Name())
if err != nil {
t.Error(err.Error())
}
if len(queries) != 3 {
t.Errorf("expected 3 queries; got %+v", len(queries))
t.Errorf("%+v", strings.Join(queries, "::"))
}
result := strings.Join(queries, ";")
if result != `select 1;select '2;2';select 3` {
t.Errorf("got unexpected results: `%+v`", result)
}
}

Просмотреть файл

@ -1,6 +1,8 @@
package text
import (
"io/ioutil"
"os"
"strings"
"testing"
)
@ -49,6 +51,43 @@ func TestParseHostsMulti(t *testing.T) {
}
}
func TestParseHostFiles(t *testing.T) {
s := `host1 host2:4408 host3`
tmpFile, err := ioutil.TempFile(os.TempDir(), "querytest-")
if err != nil {
t.Errorf("error creating temporary file: %s", err.Error())
}
defer os.Remove(tmpFile.Name())
if _, err = tmpFile.Write([]byte(s)); err != nil {
t.Errorf("error while trying to write to temporary file %s: %s", tmpFile.Name(), err.Error())
}
hosts, err := ParseHosts("", tmpFile.Name())
if err != nil {
t.Error(err.Error())
}
if len(hosts) != 3 {
t.Errorf("expected 1 host; got %+v", len(hosts))
}
if hosts[0] != `host1:3306` {
t.Errorf("got unexpected host `%+v`", hosts[0])
}
if hosts[1] != `host2:4408` {
t.Errorf("got unexpected host `%+v`", hosts[1])
}
if hosts[2] != `host3:3306` {
t.Errorf("got unexpected host `%+v`", hosts[2])
}
}
func TestSplitNonEmpty(t *testing.T) {
s := "the, quick,, brown,fox ,,"
splits := SplitNonEmpty(s, ",")