From 63451c7291b9e3a5b69c2b5ba07c493b33c54471 Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 29 Aug 2018 11:37:57 +1000 Subject: [PATCH] Gosora should now try to connect to MySQL via a pipe on Linux. --- query_gen/lib/mysql.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/query_gen/lib/mysql.go b/query_gen/lib/mysql.go index e2547899..a1c70545 100644 --- a/query_gen/lib/mysql.go +++ b/query_gen/lib/mysql.go @@ -4,6 +4,7 @@ package qgen import ( "database/sql" "errors" + "runtime" "strconv" "strings" @@ -37,6 +38,7 @@ func (adapter *MysqlAdapter) GetStmts() map[string]DBStmt { return adapter.Buffer } +// TODO: Add an option to disable unix pipes func (adapter *MysqlAdapter) BuildConn(config map[string]string) (*sql.DB, error) { dbCollation, ok := config["collation"] if !ok { @@ -47,6 +49,22 @@ func (adapter *MysqlAdapter) BuildConn(config map[string]string) (*sql.DB, error dbpassword = ":" + config["password"] } + // First try opening a pipe as those are faster + if runtime.GOOS == "linux" { + var dbsocket = "/tmp/mysql.sock" + if config["socket"] != "" { + dbsocket = config["socket"] + } + + db, err := sql.Open("mysql", config["username"]+dbpassword+"@unix("+dbsocket+")/"+config["name"]+"?collation="+dbCollation+"&parseTime=true") + if err != nil { + return db, err + } + + // Make sure that the connection is alive + return db, db.Ping() + } + // Open the database connection db, err := sql.Open("mysql", config["username"]+dbpassword+"@tcp("+config["host"]+":"+config["port"]+")/"+config["name"]+"?collation="+dbCollation+"&parseTime=true") if err != nil {