Skip to content

Commit f950c27

Browse files
committed
♻️ refactor: updated model postgres #2
1 parent c8daaa5 commit f950c27

3 files changed

Lines changed: 66 additions & 26 deletions

File tree

postgresconn/postgresconn.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,51 @@ import (
1010
"github.com/sivaosorg/govm/dbx"
1111
"github.com/sivaosorg/govm/logger"
1212
"github.com/sivaosorg/govm/postgres"
13+
"github.com/sivaosorg/govm/utils"
1314

1415
_ "github.com/lib/pq"
1516
)
1617

1718
var (
18-
instance *sqlx.DB
19+
instance *Postgres
1920
_logger = logger.NewLogger()
2021
)
2122

22-
func NewClient(config postgres.PostgresConfig) (*sqlx.DB, dbx.Dbx) {
23+
func NewPostgres() *Postgres {
24+
p := &Postgres{}
25+
return p
26+
}
27+
28+
func (p *Postgres) SetConn(value *sqlx.DB) *Postgres {
29+
p.conn = value
30+
return p
31+
}
32+
33+
func (p *Postgres) SetConfig(value postgres.PostgresConfig) *Postgres {
34+
p.Config = value
35+
return p
36+
}
37+
38+
func (p *Postgres) SetState(value dbx.Dbx) *Postgres {
39+
p.State = value
40+
return p
41+
}
42+
43+
func (p *Postgres) Close() error {
44+
return p.conn.Close()
45+
}
46+
47+
func (p *Postgres) Json() string {
48+
return utils.ToJson(p)
49+
}
50+
51+
func NewClient(config postgres.PostgresConfig) (*Postgres, dbx.Dbx) {
2352
s := dbx.NewDbx().SetDatabase(config.Database)
2453
if !config.IsEnabled {
2554
s.SetConnected(false).
2655
SetMessage("Postgres unavailable").
2756
SetError(fmt.Errorf(s.Message))
28-
return &sqlx.DB{}, *s
57+
return &Postgres{}, *s
2958
}
3059
if instance != nil {
3160
s.SetConnected(true)
@@ -53,20 +82,22 @@ func NewClient(config postgres.PostgresConfig) (*sqlx.DB, dbx.Dbx) {
5382
}
5483
client.SetMaxIdleConns(config.MaxIdleConn)
5584
client.SetMaxOpenConns(config.MaxOpenConn)
56-
instance = client
85+
instance = NewPostgres().SetConn(client)
86+
s.SetConnected(true).SetMessage("Connection established").SetNewInstance(true)
5787
if config.DebugMode {
5888
callback.MeasureTime(func() {
59-
pid, err := GetPostgresPIDConn(instance)
89+
pid, err := GetPidConn(instance)
6090
if err == nil {
6191
_logger.Info("Postgres client connection PID:: %d", pid)
6292
}
93+
s.SetPid(pid)
6394
})
6495
}
65-
s.SetConnected(true).SetMessage("Connection established")
96+
instance.SetState(*s)
6697
return instance, *s
6798
}
6899

69-
func GetPostgresPIDConn(db *sqlx.DB) (int, error) {
100+
func GetPidConn(db *Postgres) (int, error) {
70101
s := NewPostgresService(db)
71102
return s.Pid()
72103
}

postgresconn/postgresconn_model.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
package postgresconn
22

3-
import "gopkg.in/guregu/null.v3"
3+
import (
4+
"github.com/jmoiron/sqlx"
5+
"github.com/sivaosorg/govm/dbx"
6+
"github.com/sivaosorg/govm/postgres"
7+
"gopkg.in/guregu/null.v3"
8+
)
9+
10+
type Postgres struct {
11+
conn *sqlx.DB `json:"-"`
12+
Config postgres.PostgresConfig `json:"config,omitempty"`
13+
State dbx.Dbx `json:"state,omitempty"`
14+
}
415

516
type IFunctionDescriptor struct {
617
RoutineName string `db:"routine_name" json:"routine_name,omitempty"`

postgresconn/postgresconn_service.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"path/filepath"
77
"strings"
88

9-
"github.com/jmoiron/sqlx"
10-
119
_ "github.com/lib/pq"
1210
)
1311

@@ -31,10 +29,10 @@ type PostgresService interface {
3129
}
3230

3331
type postgresServiceImpl struct {
34-
dbConn *sqlx.DB
32+
dbConn *Postgres
3533
}
3634

37-
func NewPostgresService(dbConn *sqlx.DB) PostgresService {
35+
func NewPostgresService(dbConn *Postgres) PostgresService {
3836
p := &postgresServiceImpl{
3937
dbConn: dbConn,
4038
}
@@ -43,7 +41,7 @@ func NewPostgresService(dbConn *sqlx.DB) PostgresService {
4341

4442
func (p *postgresServiceImpl) Pid() (int, error) {
4543
var pid int
46-
err := p.dbConn.QueryRow("SELECT pg_backend_pid() AS pid").Scan(&pid)
44+
err := p.dbConn.conn.QueryRow("SELECT pg_backend_pid() AS pid").Scan(&pid)
4745
if err != nil {
4846
return 0, err
4947
}
@@ -52,7 +50,7 @@ func (p *postgresServiceImpl) Pid() (int, error) {
5250

5351
func (p *postgresServiceImpl) Tables() ([]string, error) {
5452
var tableNames []string
55-
err := p.dbConn.Select(&tableNames, "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'")
53+
err := p.dbConn.conn.Select(&tableNames, "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'")
5654
if err != nil {
5755
return nil, err
5856
}
@@ -61,7 +59,7 @@ func (p *postgresServiceImpl) Tables() ([]string, error) {
6159

6260
func (p *postgresServiceImpl) FunctionsDescriptor() ([]string, error) {
6361
var functions []string
64-
err := p.dbConn.Select(&functions, "SELECT routine_name FROM information_schema.routines WHERE routine_catalog = $1 AND routine_schema = 'public' AND routine_type = 'FUNCTION'", p.Database())
62+
err := p.dbConn.conn.Select(&functions, "SELECT routine_name FROM information_schema.routines WHERE routine_catalog = $1 AND routine_schema = 'public' AND routine_type = 'FUNCTION'", p.Database())
6563
if err != nil {
6664
return nil, err
6765
}
@@ -70,7 +68,7 @@ func (p *postgresServiceImpl) FunctionsDescriptor() ([]string, error) {
7068

7169
func (p *postgresServiceImpl) Database() string {
7270
var database string
73-
err := p.dbConn.Get(&database, "SELECT current_database()")
71+
err := p.dbConn.conn.Get(&database, "SELECT current_database()")
7472
if err != nil {
7573
panic(err)
7674
}
@@ -79,7 +77,7 @@ func (p *postgresServiceImpl) Database() string {
7977

8078
func (p *postgresServiceImpl) ProceduresDescriptor() ([]string, error) {
8179
var procedures []string
82-
err := p.dbConn.Select(&procedures, "SELECT routine_name FROM information_schema.routines WHERE routine_catalog = $1 AND routine_schema = 'public' AND routine_type = 'PROCEDURE'", p.Database())
80+
err := p.dbConn.conn.Select(&procedures, "SELECT routine_name FROM information_schema.routines WHERE routine_catalog = $1 AND routine_schema = 'public' AND routine_type = 'PROCEDURE'", p.Database())
8381
if err != nil {
8482
return nil, err
8583
}
@@ -88,7 +86,7 @@ func (p *postgresServiceImpl) ProceduresDescriptor() ([]string, error) {
8886

8987
func (p *postgresServiceImpl) FunctionDDescriptor(function string) ([]IFunctionDescriptor, error) {
9088
var functionDetails []IFunctionDescriptor
91-
err := p.dbConn.Select(&functionDetails, `
89+
err := p.dbConn.conn.Select(&functionDetails, `
9290
SELECT
9391
r.routine_name,
9492
p.data_type,
@@ -109,7 +107,7 @@ func (p *postgresServiceImpl) FunctionDDescriptor(function string) ([]IFunctionD
109107

110108
func (p *postgresServiceImpl) FunctionReturnType(function string) (string, error) {
111109
var returnType string
112-
err := p.dbConn.QueryRow("SELECT pg_get_function_result(oid) FROM pg_proc WHERE proname = $1", function).Scan(&returnType)
110+
err := p.dbConn.conn.QueryRow("SELECT pg_get_function_result(oid) FROM pg_proc WHERE proname = $1", function).Scan(&returnType)
113111
if err != nil {
114112
return "", err
115113
}
@@ -145,7 +143,7 @@ func (p *postgresServiceImpl) AddFunction(function string) (string, error) {
145143

146144
func (ps *postgresServiceImpl) FunctionDescriptor(function string) (string, error) {
147145
var functionContent string
148-
err := ps.dbConn.QueryRow("SELECT pg_get_functiondef($1::regproc)", function).Scan(&functionContent)
146+
err := ps.dbConn.conn.QueryRow("SELECT pg_get_functiondef($1::regproc)", function).Scan(&functionContent)
149147
if err != nil {
150148
return "", err
151149
}
@@ -154,15 +152,15 @@ func (ps *postgresServiceImpl) FunctionDescriptor(function string) (string, erro
154152

155153
func (p *postgresServiceImpl) ProcedureDescriptor(procedure string) (string, error) {
156154
var procedureContent string
157-
err := p.dbConn.QueryRow("SELECT pg_get_functiondef($1::regproc)", procedure).Scan(&procedureContent)
155+
err := p.dbConn.conn.QueryRow("SELECT pg_get_functiondef($1::regproc)", procedure).Scan(&procedureContent)
158156
if err != nil {
159157
return "", err
160158
}
161159
return procedureContent, nil
162160
}
163161

164162
func (p *postgresServiceImpl) ExplainAnalysis(query string) (string, error) {
165-
rows, err := p.dbConn.Query(fmt.Sprintf("EXPLAIN ANALYZE %v", query))
163+
rows, err := p.dbConn.conn.Query(fmt.Sprintf("EXPLAIN ANALYZE %v", query))
166164
if err != nil {
167165
return "", err
168166
}
@@ -195,7 +193,7 @@ func (p *postgresServiceImpl) ExecuteBatch(statements []string) error {
195193
if len(statements) == 0 {
196194
return fmt.Errorf("missing statements")
197195
}
198-
tx, err := p.dbConn.Beginx()
196+
tx, err := p.dbConn.conn.Beginx()
199197
if err != nil {
200198
return err
201199
}
@@ -219,7 +217,7 @@ func (p *postgresServiceImpl) ExecuteBatch(statements []string) error {
219217
}
220218

221219
func (p *postgresServiceImpl) ExecuteBatchWithTransaction(statements []string) error {
222-
tx, err := p.dbConn.Beginx()
220+
tx, err := p.dbConn.conn.Beginx()
223221
if err != nil {
224222
return err
225223
}
@@ -258,7 +256,7 @@ func (p *postgresServiceImpl) TableDescriptor(table string) ([]ITableDescriptor,
258256
FROM pg_indexes
259257
WHERE tablename = $1;
260258
`
261-
rows, err := p.dbConn.Query(s, table)
259+
rows, err := p.dbConn.conn.Query(s, table)
262260
if err != nil {
263261
return nil, err
264262
}
@@ -288,7 +286,7 @@ func (p *postgresServiceImpl) TableInfo(table string) ([]ITableInfo, error) {
288286
WHERE
289287
table_name = $1;
290288
`
291-
rows, err := p.dbConn.Query(s, table)
289+
rows, err := p.dbConn.conn.Query(s, table)
292290
if err != nil {
293291
return nil, err
294292
}

0 commit comments

Comments
 (0)