|
1 | 1 | // Copyright 2014 The Gogs Authors. All rights reserved.
|
| 2 | +// Copyright 2018 The Gitea Authors. All rights reserved. |
2 | 3 | // Use of this source code is governed by a MIT-style
|
3 | 4 | // license that can be found in the LICENSE file.
|
4 | 5 |
|
@@ -184,6 +185,18 @@ func parsePostgreSQLHostPort(info string) (string, string) {
|
184 | 185 | return host, port
|
185 | 186 | }
|
186 | 187 |
|
| 188 | +func getPostgreSQLConnectionString(DBHost, DBUser, DBPasswd, DBName, DBParam, DBSSLMode string) (connStr string) { |
| 189 | + host, port := parsePostgreSQLHostPort(DBHost) |
| 190 | + if host[0] == '/' { // looks like a unix socket |
| 191 | + connStr = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s", |
| 192 | + url.PathEscape(DBUser), url.PathEscape(DBPasswd), port, DBName, DBParam, DBSSLMode, host) |
| 193 | + } else { |
| 194 | + connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s", |
| 195 | + url.PathEscape(DBUser), url.PathEscape(DBPasswd), host, port, DBName, DBParam, DBSSLMode) |
| 196 | + } |
| 197 | + return |
| 198 | +} |
| 199 | + |
187 | 200 | func parseMSSQLHostPort(info string) (string, string) {
|
188 | 201 | host, port := "127.0.0.1", "1433"
|
189 | 202 | if strings.Contains(info, ":") {
|
@@ -214,14 +227,7 @@ func getEngine() (*xorm.Engine, error) {
|
214 | 227 | DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param)
|
215 | 228 | }
|
216 | 229 | case "postgres":
|
217 |
| - host, port := parsePostgreSQLHostPort(DbCfg.Host) |
218 |
| - if host[0] == '/' { // looks like a unix socket |
219 |
| - connStr = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s", |
220 |
| - url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), port, DbCfg.Name, Param, DbCfg.SSLMode, host) |
221 |
| - } else { |
222 |
| - connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s", |
223 |
| - url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), host, port, DbCfg.Name, Param, DbCfg.SSLMode) |
224 |
| - } |
| 230 | + connStr = getPostgreSQLConnectionString(DbCfg.Host, DbCfg.User, DbCfg.Passwd, DbCfg.Name, Param, DbCfg.SSLMode) |
225 | 231 | case "mssql":
|
226 | 232 | host, port := parseMSSQLHostPort(DbCfg.Host)
|
227 | 233 | connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, DbCfg.Name, DbCfg.User, DbCfg.Passwd)
|
|
0 commit comments