-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_config.go
More file actions
45 lines (41 loc) · 1.07 KB
/
database_config.go
File metadata and controls
45 lines (41 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package llsr
import (
"fmt"
"strings"
)
// Configuration for PostgreSQL connection.
type DatabaseConfig struct {
Database string
User string
Password string
Host string
Port int
}
// Creates new DatabaseConfiguration with given database name and User set to "postgres"
func NewDatabaseConfig(database string) *DatabaseConfig {
return &DatabaseConfig{
Database: database,
User: "postgres",
}
}
// Returns connection string that can be used in sql.Open
func (c *DatabaseConfig) ToConnectionString() string {
options := make([]string, 0)
if len(c.Database) > 0 {
options = append(options, fmt.Sprintf("dbname=%s", c.Database))
}
if len(c.User) > 0 {
options = append(options, fmt.Sprintf("user=%s", c.User))
}
if len(c.Password) > 0 {
options = append(options, fmt.Sprintf("password=%s", c.Password))
}
if len(c.Host) > 0 {
options = append(options, fmt.Sprintf("host=%s", c.Host))
}
if c.Port > 0 {
options = append(options, fmt.Sprintf("port=%d", c.Port))
}
options = append(options, "sslmode=disable")
return strings.Join(options, " ")
}