forked from andrelrg/gutils
-
Notifications
You must be signed in to change notification settings - Fork 6
/
pgsql.go
54 lines (50 loc) · 1.6 KB
/
pgsql.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
package db
import (
"fmt"
"github.com/astrolink/gutils/cache"
"log"
_ "github.com/lib/pq" //Only Drivers
)
// NewPgSQL makes a new instance of PgSQL and connect to PostgresSQL database.
func NewPgSQL(config Config) *Database {
var connectionLine string
if config.GetPassword() == "" {
connectionLine = fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable",
config.GetHost(), config.GetPort(), config.GetUser(), config.GetDatabase())
} else {
connectionLine = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
config.GetHost(), config.GetPort(), config.GetUser(), config.GetPassword(), config.GetDatabase())
}
pg := Database{
ConnectionLine: connectionLine,
Driver: "postgres",
}
var err error
err = pg.Connect()
if err != nil {
log.Fatalln(err)
}
return &pg
}
// NewPgSQL makes a new instance of PgSQL and connect to PostgresSQL database and Redis.
func NewCachedPgSQL(config Config, cacheConfig cache.Config) *Database {
var connectionLine string
if config.GetPassword() == "" {
connectionLine = fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable",
config.GetHost(), config.GetPort(), config.GetUser(), config.GetDatabase())
} else {
connectionLine = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
config.GetHost(), config.GetPort(), config.GetUser(), config.GetPassword(), config.GetDatabase())
}
pg := Database{
ConnectionLine: connectionLine,
Driver: "postgres",
CacheConfig: cacheConfig,
}
var err error
err = pg.Connect()
if err != nil {
log.Fatalln(err)
}
return &pg
}