Gosora should now try to connect to MySQL via a pipe on Linux.

This commit is contained in:
Azareal 2018-08-29 11:37:57 +10:00
parent f9dedc9691
commit 63451c7291
1 changed files with 18 additions and 0 deletions

View File

@ -4,6 +4,7 @@ package qgen
import ( import (
"database/sql" "database/sql"
"errors" "errors"
"runtime"
"strconv" "strconv"
"strings" "strings"
@ -37,6 +38,7 @@ func (adapter *MysqlAdapter) GetStmts() map[string]DBStmt {
return adapter.Buffer return adapter.Buffer
} }
// TODO: Add an option to disable unix pipes
func (adapter *MysqlAdapter) BuildConn(config map[string]string) (*sql.DB, error) { func (adapter *MysqlAdapter) BuildConn(config map[string]string) (*sql.DB, error) {
dbCollation, ok := config["collation"] dbCollation, ok := config["collation"]
if !ok { if !ok {
@ -47,6 +49,22 @@ func (adapter *MysqlAdapter) BuildConn(config map[string]string) (*sql.DB, error
dbpassword = ":" + config["password"] 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 // Open the database connection
db, err := sql.Open("mysql", config["username"]+dbpassword+"@tcp("+config["host"]+":"+config["port"]+")/"+config["name"]+"?collation="+dbCollation+"&parseTime=true") db, err := sql.Open("mysql", config["username"]+dbpassword+"@tcp("+config["host"]+":"+config["port"]+")/"+config["name"]+"?collation="+dbCollation+"&parseTime=true")
if err != nil { if err != nil {