This repository has been archived on 2022-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
tacitus/postgres/trade_service.go

192 lines
5.1 KiB
Go
Raw Normal View History

2017-09-07 23:04:47 -07:00
package postgres
import (
2017-09-09 10:14:50 -07:00
"database/sql"
2017-09-07 23:04:47 -07:00
"time"
2017-09-10 13:09:27 -07:00
"github.com/kcotugno/tacitus"
2017-09-07 23:04:47 -07:00
)
const (
2017-09-08 08:45:22 -07:00
trade_columns = `trade_id, product, price, size, buy, sell, timestamp`
2017-09-08 10:57:16 -07:00
trade_insert = `INSERT INTO trades (` + trade_columns + `) VALUES ` +
2017-09-08 08:45:22 -07:00
`($1, $2, $3, $4, $5, $6, $7) RETURNING id;`
2017-09-08 10:57:16 -07:00
trade_find = `SELECT id, ` + trade_columns + ` FROM trades WHERE id = $1;`
2017-09-08 08:45:22 -07:00
trade_find_trade_id = `SELECT id, ` + trade_columns + ` FROM trades ` +
`WHERE trade_id = $1 AND product = $2;`
2017-09-09 10:14:50 -07:00
trade_delete = `DELETE FROM trades WHERE id = $1;`
trade_in_date_range = `SELECT id, ` + trade_columns + ` FROM ` +
2017-09-08 08:45:22 -07:00
`trades WHERE product = $1 AND timestamp >= $2 AND timestamp < $3;`
trade_first = `SELECT id, ` + trade_columns + ` FROM trades WHERE ` +
2017-09-09 10:14:50 -07:00
`product = $1 ORDER BY trade_id ASC LIMIT $2;`
2017-09-08 08:45:22 -07:00
trade_last = `SELECT id, ` + trade_columns + ` FROM trades WHERE ` +
2017-09-09 10:14:50 -07:00
`product = $1 ORDER BY trade_id DESC LIMIT $2;`
trade_after = `SELECT id, ` + trade_columns + ` FROM trades ` +
`WHERE product = $1 AND trade_id < $2 ORDER BY trade_id DESC LIMIT $3;`
trade_before = `SELECT id, ` + trade_columns + ` FROM trades ` +
`WHERE product = $1 AND trade_id > $2 ORDER BY trade_id ASC LIMIT $3;`
2017-09-07 23:04:47 -07:00
)
type TradeService struct {
2017-09-10 13:04:02 -07:00
Logger tacitus.Logger
2017-09-07 23:04:47 -07:00
client *Client
}
func (t *TradeService) Trade(id int) (*tacitus.Trade, error) {
var tr tacitus.Trade
2017-09-10 13:04:02 -07:00
t.logQuery(trade_find, id)
2017-09-07 23:04:47 -07:00
row := t.client.db.QueryRow(trade_find, id)
if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy,
&tr.Sell, &tr.Timestamp); err != nil {
2017-09-10 13:04:02 -07:00
t.logError(trade_find, err, id)
2017-09-07 23:04:47 -07:00
return nil, err
}
return &tr, nil
}
func (t *TradeService) TradeByTradeId(id int, prod string) (*tacitus.Trade, error) {
var tr tacitus.Trade
2017-09-10 13:04:02 -07:00
t.logQuery(trade_find_trade_id, id, prod)
2017-09-07 23:04:47 -07:00
row := t.client.db.QueryRow(trade_find_trade_id, id, prod)
2017-09-08 08:45:22 -07:00
if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price,
&tr.Size, &tr.Buy, &tr.Sell, &tr.Timestamp); err != nil {
2017-09-07 23:04:47 -07:00
2017-09-10 13:04:02 -07:00
t.logError(trade_find_trade_id, err, id, prod)
2017-09-07 23:04:47 -07:00
return nil, err
}
return &tr, nil
}
func (t *TradeService) CreateTrade(trade *tacitus.Trade) error {
var id int
2017-09-10 13:04:02 -07:00
params := []interface{}{trade.TradeId, trade.Product, trade.Price,
trade.Size, trade.Buy, trade.Sell, trade.Timestamp}
t.logQuery(trade_insert, params...)
res := t.client.db.QueryRow(trade_insert, params...)
2017-09-07 23:04:47 -07:00
if err := res.Scan(&id); err != nil {
2017-09-10 13:04:02 -07:00
t.logError(trade_insert, err, params...)
2017-09-07 23:04:47 -07:00
return err
}
trade.Id = id
return nil
}
func (t *TradeService) DeleteTrade(id int) {
2017-09-10 13:04:02 -07:00
t.logQuery(trade_delete, id)
_, err := t.client.db.Exec(trade_delete, id)
2017-09-23 11:00:07 -07:00
if err != nil {
2017-09-10 13:04:02 -07:00
t.logError(trade_delete, err, id)
}
2017-09-07 23:04:47 -07:00
}
func (t *TradeService) TradesInDateRange(product string, start,
end time.Time) ([]tacitus.Trade, error) {
2017-09-10 13:04:02 -07:00
t.logQuery(trade_in_date_range, product, start, end)
2017-09-09 10:14:50 -07:00
rows, err := t.client.db.Query(trade_in_date_range, product, start, end)
2017-09-07 23:04:47 -07:00
if err != nil {
2017-09-10 13:04:02 -07:00
t.logError(trade_in_date_range, err, product, start, end)
2017-09-07 23:04:47 -07:00
return nil, err
}
2017-09-09 10:14:50 -07:00
return deserializeTrades(rows)
}
2017-09-07 23:04:47 -07:00
2017-09-09 10:14:50 -07:00
func (t *TradeService) FirstTrades(product string, limit int) ([]tacitus.Trade, error) {
2017-09-10 13:04:02 -07:00
t.logQuery(trade_first, product, limit)
2017-09-09 10:14:50 -07:00
rows, err := t.client.db.Query(trade_first, product, limit)
if err != nil {
2017-09-10 13:04:02 -07:00
t.logError(trade_first, err, product, limit)
2017-09-09 10:14:50 -07:00
return nil, err
2017-09-07 23:04:47 -07:00
}
2017-09-09 10:14:50 -07:00
return deserializeTrades(rows)
}
func (t *TradeService) LastTrades(product string, limit int) ([]tacitus.Trade, error) {
2017-09-10 13:04:02 -07:00
t.logQuery(trade_last, product, limit)
2017-09-09 10:14:50 -07:00
rows, err := t.client.db.Query(trade_last, product, limit)
if err != nil {
2017-09-10 13:04:02 -07:00
t.logError(trade_last, err, product, limit)
2017-09-09 10:14:50 -07:00
return nil, err
2017-09-07 23:04:47 -07:00
}
2017-09-09 10:14:50 -07:00
return deserializeTrades(rows)
2017-09-07 23:04:47 -07:00
}
2017-09-08 07:35:47 -07:00
2017-09-09 10:14:50 -07:00
func (t *TradeService) TradesAfter(product string, id, limit int) ([]tacitus.Trade, error) {
2017-09-10 13:04:02 -07:00
t.logQuery(trade_after, product, id, limit)
2017-09-09 10:14:50 -07:00
rows, err := t.client.db.Query(trade_after, product, id, limit)
if err != nil {
2017-09-10 13:04:02 -07:00
t.logError(trade_after, err, product, id, limit)
2017-09-09 10:14:50 -07:00
return nil, err
}
2017-09-08 07:35:47 -07:00
2017-09-09 10:14:50 -07:00
return deserializeTrades(rows)
2017-09-08 07:35:47 -07:00
2017-09-09 10:14:50 -07:00
}
func (t *TradeService) TradesBefore(product string, id, limit int) ([]tacitus.Trade, error) {
2017-09-10 13:04:02 -07:00
t.logQuery(trade_before, product, id, limit)
2017-09-09 10:14:50 -07:00
rows, err := t.client.db.Query(trade_before, product, id, limit)
if err != nil {
2017-09-10 13:04:02 -07:00
t.logError(trade_before, err, product, id, limit)
2017-09-08 07:35:47 -07:00
return nil, err
}
2017-09-09 10:14:50 -07:00
return deserializeTrades(rows)
2017-09-08 07:35:47 -07:00
}
2017-09-10 13:04:02 -07:00
func (t *TradeService) logQuery(sql string, params ...interface{}) {
t.log(`SQL query="%v" params="%v"`, sql, params)
}
func (t *TradeService) logError(sql string, err error, params ...interface{}) {
t.log(`SQL FAIL query="%v" params="%v" error="%v"`, sql, params, err)
}
func (t *TradeService) log(format string, params ...interface{}) {
if t.Logger != nil {
t.Logger.Info(format, params...)
}
}
2017-09-09 10:14:50 -07:00
func deserializeTrades(rows *sql.Rows) ([]tacitus.Trade, error) {
defer rows.Close()
2017-09-08 07:35:47 -07:00
2017-09-09 10:14:50 -07:00
trades := make([]tacitus.Trade, 0)
2017-09-08 07:35:47 -07:00
2017-09-09 10:14:50 -07:00
for rows.Next() {
var trade tacitus.Trade
if err := rows.Scan(&trade.Id, &trade.TradeId, &trade.Product, &trade.Price,
&trade.Size, &trade.Buy, &trade.Sell, &trade.Timestamp); err != nil {
return nil, err
}
trades = append(trades, trade)
2017-09-08 07:35:47 -07:00
}
2017-09-09 10:14:50 -07:00
if rows.Err() != nil {
return nil, rows.Err()
}
return trades, nil
2017-09-08 07:35:47 -07:00
}