2017-09-07 23:04:47 -07:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
|
|
|
|
"bytes"
|
|
|
|
"database/sql"
|
|
|
|
"text/template"
|
|
|
|
|
2017-09-10 13:09:27 -07:00
|
|
|
"github.com/kcotugno/tacitus"
|
2017-09-07 23:04:47 -07:00
|
|
|
)
|
|
|
|
|
2018-01-11 10:10:12 -07:00
|
|
|
const connStr = `host={{.Host}} port={{.Port}} user={{.User}} ` +
|
|
|
|
`password={{.Password}} dbname={{.Name}} sslmode={{.SslMode}}`
|
2017-09-07 23:04:47 -07:00
|
|
|
|
|
|
|
type Client struct {
|
2018-01-11 10:10:12 -07:00
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Name string
|
|
|
|
User string
|
|
|
|
SslMode string
|
|
|
|
Password string
|
2017-09-07 23:04:47 -07:00
|
|
|
|
2017-10-05 21:53:41 -07:00
|
|
|
tradeService TradeService
|
2017-10-05 21:53:02 -07:00
|
|
|
confirmationService ConfirmationService
|
2017-10-08 11:19:42 -07:00
|
|
|
aggregationService AggregationService
|
2017-09-07 23:04:47 -07:00
|
|
|
|
2017-09-10 13:04:02 -07:00
|
|
|
logger tacitus.Logger
|
2017-09-23 11:00:07 -07:00
|
|
|
db *sql.DB
|
2017-09-07 23:04:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient() *Client {
|
|
|
|
c := Client{}
|
|
|
|
c.tradeService.client = &c
|
2017-10-05 21:53:02 -07:00
|
|
|
c.confirmationService.client = &c
|
2017-10-08 11:19:42 -07:00
|
|
|
c.aggregationService.client = &c
|
2018-01-11 10:10:12 -07:00
|
|
|
|
|
|
|
c.Host = "localhost"
|
|
|
|
c.Port = 5432
|
2017-09-07 23:04:47 -07:00
|
|
|
c.SslMode = "disable"
|
|
|
|
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
2017-09-10 13:04:02 -07:00
|
|
|
func (c *Client) SetLogger(logger tacitus.Logger) {
|
|
|
|
c.logger = logger
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:04:47 -07:00
|
|
|
func (c *Client) Open() error {
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
tmpl, _ := template.New("conn").Parse(connStr)
|
|
|
|
tmpl.Execute(&buf, c)
|
|
|
|
|
|
|
|
db, err := sql.Open("postgres", buf.String())
|
|
|
|
if err != nil {
|
2017-09-10 13:04:02 -07:00
|
|
|
c.log(`Failed to connect to postgres. params=%v`, buf.String())
|
2017-09-07 23:04:47 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.Ping(); err != nil {
|
2017-09-10 13:04:02 -07:00
|
|
|
c.log(`Failed to connect to postgres. params=%v`, buf.String())
|
2017-09-07 23:04:47 -07:00
|
|
|
return err
|
|
|
|
}
|
2017-09-10 13:04:02 -07:00
|
|
|
c.log(`Conntected to postgres. params="%v"`, buf.String())
|
2017-09-07 23:04:47 -07:00
|
|
|
|
|
|
|
c.db = db
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-10 13:04:02 -07:00
|
|
|
func (c *Client) Close() error {
|
|
|
|
err := c.db.Close()
|
|
|
|
c.log("Closed postgres connection")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:04:47 -07:00
|
|
|
func (c *Client) TradeService() tacitus.TradeService {
|
|
|
|
return &c.tradeService
|
|
|
|
}
|
2017-09-10 13:04:02 -07:00
|
|
|
|
2017-10-05 21:53:02 -07:00
|
|
|
func (c *Client) ConfirmationService() tacitus.ConfirmationService {
|
|
|
|
return &c.confirmationService
|
|
|
|
}
|
|
|
|
|
2017-10-08 11:19:42 -07:00
|
|
|
func (c *Client) AggregationService() tacitus.AggregationService {
|
|
|
|
return &c.aggregationService
|
|
|
|
}
|
|
|
|
|
2017-09-10 13:04:02 -07:00
|
|
|
func (c *Client) log(format string, params ...interface{}) {
|
|
|
|
if c.logger != nil {
|
|
|
|
c.logger.Info(format, params...)
|
|
|
|
}
|
|
|
|
}
|
2017-09-23 11:07:50 -07:00
|
|
|
|
|
|
|
func (c *Client) logQuery(sql string, params ...interface{}) {
|
|
|
|
c.log(`SQL query="%v" params="%v"`, sql, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) logError(sql string, err error, params ...interface{}) {
|
|
|
|
c.log(`SQL FAIL query="%v" params="%v" error="%v"`, sql, params, err)
|
|
|
|
}
|