#3013 support connect PostgreSQL via unix socket

This commit is contained in:
Unknwon 2016-08-11 14:38:26 -07:00
parent 53c573ed02
commit 5850308a37
1 changed files with 19 additions and 12 deletions

View File

@ -6,6 +6,7 @@ package models
import ( import (
"database/sql" "database/sql"
"errors"
"fmt" "fmt"
"net/url" "net/url"
"os" "os"
@ -97,7 +98,7 @@ func LoadConfigs() {
} }
func getEngine() (*xorm.Engine, error) { func getEngine() (*xorm.Engine, error) {
cnnstr := "" connStr := ""
var Param string = "?" var Param string = "?"
if strings.Contains(DbCfg.Name, Param) { if strings.Contains(DbCfg.Name, Param) {
Param = "&" Param = "&"
@ -105,14 +106,14 @@ func getEngine() (*xorm.Engine, error) {
switch DbCfg.Type { switch DbCfg.Type {
case "mysql": case "mysql":
if DbCfg.Host[0] == '/' { // looks like a unix socket 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) DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param)
} else { } 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) DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param)
} }
case "postgres": case "postgres":
var host, port = "127.0.0.1", "5432" host, port := "127.0.0.1", "5432"
fields := strings.Split(DbCfg.Host, ":") fields := strings.Split(DbCfg.Host, ":")
if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 { if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
host = fields[0] host = fields[0]
@ -120,28 +121,34 @@ func getEngine() (*xorm.Engine, error) {
if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 { if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
port = fields[1] 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": case "sqlite3":
if !EnableSQLite3 { 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 { if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil {
return nil, fmt.Errorf("Fail to create directories: %v", err) 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": case "tidb":
if !EnableTidb { if !EnableTiDB {
return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type) 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 { if err := os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm); err != nil {
return nil, fmt.Errorf("Fail to create directories: %v", err) return nil, fmt.Errorf("Fail to create directories: %v", err)
} }
cnnstr = "goleveldb://" + DbCfg.Path connStr = "goleveldb://" + DbCfg.Path
default: default:
return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type) 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) { func NewTestEngine(x *xorm.Engine) (err error) {