-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpsqlconn.go
107 lines (94 loc) · 2.56 KB
/
psqlconn.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package psqlconn
import (
"context"
"fmt"
"os"
"github.com/jmoiron/sqlx"
"github.com/sivaosorg/govm/callback"
"github.com/sivaosorg/govm/common"
"github.com/sivaosorg/govm/dbx"
"github.com/sivaosorg/govm/logger"
"github.com/sivaosorg/govm/postgres"
"github.com/sivaosorg/govm/utils"
_ "github.com/lib/pq"
)
var (
_logger = logger.NewLogger()
)
func NewPostgres() *Postgres {
p := &Postgres{}
return p
}
func (p *Postgres) SetConn(value *sqlx.DB) *Postgres {
p.conn = value
return p
}
func (p *Postgres) SetConfig(value postgres.PostgresConfig) *Postgres {
p.Config = value
return p
}
func (p *Postgres) SetState(value dbx.Dbx) *Postgres {
p.State = value
return p
}
func (p *Postgres) Close() error {
return p.conn.Close()
}
func (p *Postgres) Json() string {
return utils.ToJson(p)
}
func (p *Postgres) GetConn() *sqlx.DB {
return p.conn
}
func NewClient(config postgres.PostgresConfig) (*Postgres, dbx.Dbx) {
instance := NewPostgres()
s := dbx.NewDbx().SetDatabase(config.Database)
if !config.IsEnabled {
s.SetConnected(false).
SetMessage("Postgres unavailable").
SetError(fmt.Errorf(s.Message))
instance.SetState(*s)
return instance, *s
}
stringConn := fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s",
config.Host, config.Port, config.Username, config.Database, config.Password, config.SSLMode)
if config.DebugMode {
_logger.Info(fmt.Sprintf("Postgres client connection:: %s", stringConn))
}
client, err := sqlx.Open(common.EntryKeyPostgres, stringConn)
if err != nil {
s.SetError(err).SetConnected(false).SetMessage(err.Error())
instance.SetState(*s)
return instance, *s
}
ctx, cancel := context.WithTimeout(context.Background(), config.Timeout)
defer cancel()
err = client.PingContext(ctx)
if err != nil {
s.SetError(err).SetConnected(false).SetMessage(err.Error())
instance.SetState(*s)
return instance, *s
}
if config.DebugMode {
_logger.Info(fmt.Sprintf("Connected successfully to postgres database %s:%d/%s", config.Host, config.Port, config.Database))
}
client.SetMaxIdleConns(config.MaxIdleConn)
client.SetMaxOpenConns(config.MaxOpenConn)
instance.SetConn(client)
s.SetConnected(true).SetMessage("Connected successfully").SetNewInstance(true).SetPid(os.Getpid())
if config.DebugMode {
callback.MeasureTime(func() {
pid, err := GetPidConn(instance)
if err == nil {
_logger.Info("Postgres client connection PID:: %d", pid)
}
s.SetPid(pid)
})
}
instance.SetState(*s)
return instance, *s
}
func GetPidConn(db *Postgres) (int, error) {
s := NewPostgresService(db)
return s.Pid()
}