From 56c2f082fff609c26709bdb749cb5f0efa36b7ee Mon Sep 17 00:00:00 2001 From: Jacky Zhen Date: Tue, 23 Oct 2018 21:59:50 +1300 Subject: [PATCH] Make postgres output consistent with mysql --- main_test.go | 18 +++++++++--------- sql_runner.go | 9 ++++++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/main_test.go b/main_test.go index b23ad29..48af77b 100644 --- a/main_test.go +++ b/main_test.go @@ -187,15 +187,15 @@ func Test_PostgreSQL(t *testing.T) { query: "SELECT id, name FROM table1", expected: []string{ "", - "db1\t1 | John", - "db1\t2 | George", - "db1\t3 | Richard", - "db2\t1 | Rob", - "db2\t2 | Ken", - "db2\t3 | Robert", - "db3\t1 | Athos", - "db3\t2 | Porthos", - "db3\t3 | Aramis", + "db1\t1\tJohn", + "db1\t2\tGeorge", + "db1\t3\tRichard", + "db2\t1\tRob", + "db2\t2\tKen", + "db2\t3\tRobert", + "db3\t1\tAthos", + "db3\t2\tPorthos", + "db3\t3\tAramis", }, }, } diff --git a/sql_runner.go b/sql_runner.go index a43c5b9..ddeef73 100644 --- a/sql_runner.go +++ b/sql_runner.go @@ -49,7 +49,7 @@ var sqlTypeToOptions = map[sqlType]sqlOptions{ "-h%v", "PGPASSWORD=%v", "-d %v", - "-tc", + "-tAc", }, } @@ -109,10 +109,17 @@ func (sr *sqlRunner) runSQL(db database, key string) bool { var cmd *exec.Cmd if db.AppServer != "" { escapedQuery := fmt.Sprintf(`'%v'`, strings.Replace(sr.query, `'`, `'"'"'`, -1)) + if sr.typ == postgreSQL { + escapedQuery += fmt.Sprintf("-F%s", "\t") + } + cmd = exec.CommandContext(sr.quitContext, "ssh", db.AppServer, options+escapedQuery) } else { args := append(trimEmpty(strings.Split(options, " ")), sr.query) + if sr.typ == postgreSQL { + args = append(args, fmt.Sprintf("-F%s", "\t")) + } cmd = exec.CommandContext(sr.quitContext, args[0], args[1:]...) }