From 5850308a3785b440872409e76da69acdebaca5da Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 11 Aug 2016 14:38:26 -0700 Subject: [PATCH] #3013 support connect PostgreSQL via unix socket --- models/models.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/models/models.go b/models/models.go index 6a3abc54b3..a8671c9f9b 100644 --- a/models/models.go +++ b/models/models.go @@ -6,6 +6,7 @@ package models import ( "database/sql" + "errors" "fmt" "net/url" "os" @@ -97,7 +98,7 @@ func LoadConfigs() { } func getEngine() (*xorm.Engine, error) { - cnnstr := "" + connStr := "" var Param string = "?" if strings.Contains(DbCfg.Name, Param) { Param = "&" @@ -105,14 +106,14 @@ func getEngine() (*xorm.Engine, error) { switch DbCfg.Type { case "mysql": if DbCfg.Host[0] == '/' { // looks like a unix socket - cnnstr = fmt.Sprintf("%s:%s@unix(%s)/%s%scharset=utf8&parseTime=true", + connStr = fmt.Sprintf("%s:%s@unix(%s)/%s%scharset=utf8&parseTime=true", DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param) } else { - cnnstr = fmt.Sprintf("%s:%s@tcp(%s)/%s%scharset=utf8&parseTime=true", + connStr = fmt.Sprintf("%s:%s@tcp(%s)/%s%scharset=utf8&parseTime=true", DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param) } case "postgres": - var host, port = "127.0.0.1", "5432" + host, port := "127.0.0.1", "5432" fields := strings.Split(DbCfg.Host, ":") if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 { host = fields[0] @@ -120,28 +121,34 @@ func getEngine() (*xorm.Engine, error) { if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 { port = fields[1] } - cnnstr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s", - url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), host, port, DbCfg.Name, Param, DbCfg.SSLMode) + + if host[0] == '/' { // looks like a unix socket + connStr = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s", + url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), port, DbCfg.Name, Param, DbCfg.SSLMode, host) + } else { + connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s", + url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), host, port, DbCfg.Name, Param, DbCfg.SSLMode) + } case "sqlite3": if !EnableSQLite3 { - return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type) + return nil, errors.New("This binary version does not build support for SQLite3.") } if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil { return nil, fmt.Errorf("Fail to create directories: %v", err) } - cnnstr = "file:" + DbCfg.Path + "?cache=shared&mode=rwc" + connStr = "file:" + DbCfg.Path + "?cache=shared&mode=rwc" case "tidb": - if !EnableTidb { - return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type) + if !EnableTiDB { + return nil, errors.New("This binary version does not build support for TiDB.") } if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil { return nil, fmt.Errorf("Fail to create directories: %v", err) } - cnnstr = "goleveldb://" + DbCfg.Path + connStr = "goleveldb://" + DbCfg.Path default: return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type) } - return xorm.NewEngine(DbCfg.Type, cnnstr) + return xorm.NewEngine(DbCfg.Type, connStr) } func NewTestEngine(x *xorm.Engine) (err error) {