-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
82 lines (70 loc) · 1.81 KB
/
connection.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package database
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
// establish connection to the database
type ConnectionExecutor interface {
Connect() error
Close() error
GetDB() *sql.DB
}
// concrete implementation of ConnectionExecutor
type postgresConnection struct {
db *sql.DB // db instance handle
Host string `json:"host"`
DbName string `json:"dbname"`
Port string `json:"port"`
User string `json:"user"`
Pass string `json:"pass"`
Ssl string `json:"ssl"`
}
// connect to the postgres db
func (c *postgresConnection) Connect() error {
if c.Host == "" {
return fmt.Errorf("host not set")
} else if c.DbName == "" {
return fmt.Errorf("database not set")
} else if c.Port == "" {
return fmt.Errorf("port not set")
} else if c.User == "" {
return fmt.Errorf("user not set")
} else if c.Pass == "" {
return fmt.Errorf("creds not set")
} else if c.Ssl == "" {
return fmt.Errorf("ssl mode not set")
}
dsn := fmt.Sprintf("host=%s dbname=%s user=%s password=%s port=%s sslmode=%s", c.Host, c.DbName, c.User, c.Pass, c.Port, c.Ssl)
// open a connection to postgres
db, err := sql.Open("postgres", dsn)
if err != nil {
return fmt.Errorf("failed to connect to db => %s", err.Error())
}
// verify the connection
err = db.Ping()
if err != nil {
return fmt.Errorf("failed to ping db => %s", err.Error())
}
c.db = db
return nil
}
// close the connection to the postgres db
func (c *postgresConnection) Close() error {
return c.db.Close()
}
// get a handle to the db instance
func (c *postgresConnection) GetDB() *sql.DB {
return c.db
}
// constructor for orm
func NewConnection(host, dbname, user, pass, port, ssl string) ConnectionExecutor {
return &postgresConnection{
Host: host,
DbName: dbname,
User: user,
Pass: pass,
Port: port,
Ssl: ssl,
}
}