-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_manager.go
56 lines (46 loc) · 997 Bytes
/
connection_manager.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
package db_migrator
import (
"database/sql"
_ "github.com/lib/pq"
)
type Db struct {
Connection *sql.DB
}
type ConnectionString interface {
GetConnectionStringWithoutDB() (string, error)
GetConnectionString() (string, error)
}
func (db *Db) Connect(connectionString ConnectionString) error {
conStr, err := connectionString.GetConnectionString()
if err != nil {
return err
}
db_, err := sql.Open("postgres", conStr)
if err != nil {
return err
}
db.Connection = db_
return nil
}
func (db *Db) ConnectWithoutDB(connectionString ConnectionString) error {
conStr, err := connectionString.GetConnectionString()
if err != nil {
return err
}
db_, err := sql.Open("postgres", conStr)
if err != nil {
return err
}
db.Connection = db_
return nil
}
func (db *Db) Disconnect() error {
return db.Connection.Close()
}
func (db Db) Query(query string) (*sql.Rows, error) {
rows, err := db.Connection.Query(query)
if err != nil {
return nil, err
}
return rows, nil
}