Skip to content

Commit

Permalink
♻️ refactor: updated model postgres #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Oct 24, 2023
1 parent c8daaa5 commit f950c27
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 26 deletions.
45 changes: 38 additions & 7 deletions postgresconn/postgresconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,51 @@ import (
"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 (
instance *sqlx.DB
instance *Postgres
_logger = logger.NewLogger()
)

func NewClient(config postgres.PostgresConfig) (*sqlx.DB, dbx.Dbx) {
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 NewClient(config postgres.PostgresConfig) (*Postgres, dbx.Dbx) {
s := dbx.NewDbx().SetDatabase(config.Database)
if !config.IsEnabled {
s.SetConnected(false).
SetMessage("Postgres unavailable").
SetError(fmt.Errorf(s.Message))
return &sqlx.DB{}, *s
return &Postgres{}, *s
}
if instance != nil {
s.SetConnected(true)
Expand Down Expand Up @@ -53,20 +82,22 @@ func NewClient(config postgres.PostgresConfig) (*sqlx.DB, dbx.Dbx) {
}
client.SetMaxIdleConns(config.MaxIdleConn)
client.SetMaxOpenConns(config.MaxOpenConn)
instance = client
instance = NewPostgres().SetConn(client)
s.SetConnected(true).SetMessage("Connection established").SetNewInstance(true)
if config.DebugMode {
callback.MeasureTime(func() {
pid, err := GetPostgresPIDConn(instance)
pid, err := GetPidConn(instance)
if err == nil {
_logger.Info("Postgres client connection PID:: %d", pid)
}
s.SetPid(pid)
})
}
s.SetConnected(true).SetMessage("Connection established")
instance.SetState(*s)
return instance, *s
}

func GetPostgresPIDConn(db *sqlx.DB) (int, error) {
func GetPidConn(db *Postgres) (int, error) {
s := NewPostgresService(db)
return s.Pid()
}
13 changes: 12 additions & 1 deletion postgresconn/postgresconn_model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package postgresconn

import "gopkg.in/guregu/null.v3"
import (
"github.com/jmoiron/sqlx"
"github.com/sivaosorg/govm/dbx"
"github.com/sivaosorg/govm/postgres"
"gopkg.in/guregu/null.v3"
)

type Postgres struct {
conn *sqlx.DB `json:"-"`
Config postgres.PostgresConfig `json:"config,omitempty"`
State dbx.Dbx `json:"state,omitempty"`
}

type IFunctionDescriptor struct {
RoutineName string `db:"routine_name" json:"routine_name,omitempty"`
Expand Down
34 changes: 16 additions & 18 deletions postgresconn/postgresconn_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"path/filepath"
"strings"

"github.com/jmoiron/sqlx"

_ "github.com/lib/pq"
)

Expand All @@ -31,10 +29,10 @@ type PostgresService interface {
}

type postgresServiceImpl struct {
dbConn *sqlx.DB
dbConn *Postgres
}

func NewPostgresService(dbConn *sqlx.DB) PostgresService {
func NewPostgresService(dbConn *Postgres) PostgresService {
p := &postgresServiceImpl{
dbConn: dbConn,
}
Expand All @@ -43,7 +41,7 @@ func NewPostgresService(dbConn *sqlx.DB) PostgresService {

func (p *postgresServiceImpl) Pid() (int, error) {
var pid int
err := p.dbConn.QueryRow("SELECT pg_backend_pid() AS pid").Scan(&pid)
err := p.dbConn.conn.QueryRow("SELECT pg_backend_pid() AS pid").Scan(&pid)
if err != nil {
return 0, err
}
Expand All @@ -52,7 +50,7 @@ func (p *postgresServiceImpl) Pid() (int, error) {

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

func (p *postgresServiceImpl) FunctionsDescriptor() ([]string, error) {
var functions []string
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())
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())
if err != nil {
return nil, err
}
Expand All @@ -70,7 +68,7 @@ func (p *postgresServiceImpl) FunctionsDescriptor() ([]string, error) {

func (p *postgresServiceImpl) Database() string {
var database string
err := p.dbConn.Get(&database, "SELECT current_database()")
err := p.dbConn.conn.Get(&database, "SELECT current_database()")
if err != nil {
panic(err)
}
Expand All @@ -79,7 +77,7 @@ func (p *postgresServiceImpl) Database() string {

func (p *postgresServiceImpl) ProceduresDescriptor() ([]string, error) {
var procedures []string
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())
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())
if err != nil {
return nil, err
}
Expand All @@ -88,7 +86,7 @@ func (p *postgresServiceImpl) ProceduresDescriptor() ([]string, error) {

func (p *postgresServiceImpl) FunctionDDescriptor(function string) ([]IFunctionDescriptor, error) {
var functionDetails []IFunctionDescriptor
err := p.dbConn.Select(&functionDetails, `
err := p.dbConn.conn.Select(&functionDetails, `
SELECT
r.routine_name,
p.data_type,
Expand All @@ -109,7 +107,7 @@ func (p *postgresServiceImpl) FunctionDDescriptor(function string) ([]IFunctionD

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

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

func (p *postgresServiceImpl) ProcedureDescriptor(procedure string) (string, error) {
var procedureContent string
err := p.dbConn.QueryRow("SELECT pg_get_functiondef($1::regproc)", procedure).Scan(&procedureContent)
err := p.dbConn.conn.QueryRow("SELECT pg_get_functiondef($1::regproc)", procedure).Scan(&procedureContent)
if err != nil {
return "", err
}
return procedureContent, nil
}

func (p *postgresServiceImpl) ExplainAnalysis(query string) (string, error) {
rows, err := p.dbConn.Query(fmt.Sprintf("EXPLAIN ANALYZE %v", query))
rows, err := p.dbConn.conn.Query(fmt.Sprintf("EXPLAIN ANALYZE %v", query))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -195,7 +193,7 @@ func (p *postgresServiceImpl) ExecuteBatch(statements []string) error {
if len(statements) == 0 {
return fmt.Errorf("missing statements")
}
tx, err := p.dbConn.Beginx()
tx, err := p.dbConn.conn.Beginx()
if err != nil {
return err
}
Expand All @@ -219,7 +217,7 @@ func (p *postgresServiceImpl) ExecuteBatch(statements []string) error {
}

func (p *postgresServiceImpl) ExecuteBatchWithTransaction(statements []string) error {
tx, err := p.dbConn.Beginx()
tx, err := p.dbConn.conn.Beginx()
if err != nil {
return err
}
Expand Down Expand Up @@ -258,7 +256,7 @@ func (p *postgresServiceImpl) TableDescriptor(table string) ([]ITableDescriptor,
FROM pg_indexes
WHERE tablename = $1;
`
rows, err := p.dbConn.Query(s, table)
rows, err := p.dbConn.conn.Query(s, table)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -288,7 +286,7 @@ func (p *postgresServiceImpl) TableInfo(table string) ([]ITableInfo, error) {
WHERE
table_name = $1;
`
rows, err := p.dbConn.Query(s, table)
rows, err := p.dbConn.conn.Query(s, table)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit f950c27

Please sign in to comment.