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

178 lines
4.9 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-23 16:52:47 -07:00
`($1, $2, $3, $4, $5, $6, $7) RETURNING id,` + trade_columns + `;`
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 {
client *Client
}
2017-09-23 16:52:47 -07:00
func (s *TradeService) Trade(id int) (tacitus.Trade, error) {
var t tacitus.Trade
2017-09-07 23:04:47 -07:00
2017-09-23 16:52:47 -07:00
s.client.logQuery(trade_find, id)
row := s.client.db.QueryRow(trade_find, id)
err := row.Scan(&t.Id, &t.TradeId, &t.Product, &t.Price, &t.Size, &t.Buy,
&t.Sell, &t.Timestamp)
if err != nil {
s.client.logError(trade_find, err, id)
2017-09-07 23:04:47 -07:00
2017-09-23 16:52:47 -07:00
return t, err
2017-09-07 23:04:47 -07:00
}
2017-09-23 16:52:47 -07:00
return t, nil
2017-09-07 23:04:47 -07:00
}
2017-09-23 16:52:47 -07:00
func (s *TradeService) TradeByTradeId(id int, prod string) (tacitus.Trade, error) {
var t tacitus.Trade
2017-09-07 23:04:47 -07:00
2017-09-23 16:52:47 -07:00
s.client.logQuery(trade_find_trade_id, id, prod)
row := s.client.db.QueryRow(trade_find_trade_id, id, prod)
err := row.Scan(&t.Id, &t.TradeId, &t.Product, &t.Price,
&t.Size, &t.Buy, &t.Sell, &t.Timestamp)
if err != nil {
s.client.logError(trade_find_trade_id, err, id, prod)
2017-09-10 13:04:02 -07:00
2017-09-23 16:52:47 -07:00
return t, err
2017-09-07 23:04:47 -07:00
}
2017-09-23 16:52:47 -07:00
return t, nil
2017-09-07 23:04:47 -07:00
}
2017-09-23 16:52:47 -07:00
func (s *TradeService) CreateTrade(trade tacitus.Trade) (tacitus.Trade, error) {
var t tacitus.Trade
2017-09-10 13:04:02 -07:00
params := []interface{}{trade.TradeId, trade.Product, trade.Price,
trade.Size, trade.Buy, trade.Sell, trade.Timestamp}
2017-09-23 16:52:47 -07:00
s.client.logQuery(trade_insert, params...)
res := s.client.db.QueryRow(trade_insert, params...)
2017-09-24 17:30:01 -07:00
if err := res.Scan(&t.Id, &t.TradeId, &t.Product, &t.Price, &t.Size,
&t.Buy, &t.Sell, &t.Timestamp); err != nil {
2017-09-23 16:52:47 -07:00
s.client.logError(trade_insert, err, params...)
return t, err
2017-09-07 23:04:47 -07:00
}
2017-09-23 16:52:47 -07:00
return t, nil
2017-09-07 23:04:47 -07:00
}
2017-09-23 16:52:47 -07:00
func (s *TradeService) DeleteTrade(id int) {
s.client.logQuery(trade_delete, id)
2017-09-10 13:04:02 -07:00
2017-09-23 16:52:47 -07:00
_, err := s.client.db.Exec(trade_delete, id)
2017-09-23 11:00:07 -07:00
if err != nil {
2017-09-23 16:52:47 -07:00
s.client.logError(trade_delete, err, id)
2017-09-10 13:04:02 -07:00
}
2017-09-07 23:04:47 -07:00
}
2017-09-23 16:52:47 -07:00
func (s *TradeService) TradesInDateRange(product string, start,
2017-09-07 23:04:47 -07:00
end time.Time) ([]tacitus.Trade, error) {
2017-09-23 16:52:47 -07:00
s.client.logQuery(trade_in_date_range, product, start, end)
rows, err := s.client.db.Query(trade_in_date_range, product, start, end)
2017-09-07 23:04:47 -07:00
if err != nil {
2017-09-23 16:52:47 -07:00
s.client.logError(trade_in_date_range, err, product, start, end)
2017-09-10 13:04:02 -07:00
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-23 16:52:47 -07:00
func (s *TradeService) FirstTrades(product string, limit int) ([]tacitus.Trade, error) {
s.client.logQuery(trade_first, product, limit)
rows, err := s.client.db.Query(trade_first, product, limit)
2017-09-09 10:14:50 -07:00
if err != nil {
2017-09-23 16:52:47 -07:00
s.client.logError(trade_first, err, product, limit)
2017-09-10 13:04:02 -07:00
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-23 16:52:47 -07:00
func (s *TradeService) LastTrades(product string, limit int) ([]tacitus.Trade, error) {
s.client.logQuery(trade_last, product, limit)
rows, err := s.client.db.Query(trade_last, product, limit)
2017-09-09 10:14:50 -07:00
if err != nil {
2017-09-23 16:52:47 -07:00
s.client.logError(trade_last, err, product, limit)
2017-09-10 13:04:02 -07:00
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-23 16:52:47 -07:00
func (s *TradeService) TradesAfter(product string, id, limit int) ([]tacitus.Trade, error) {
s.client.logQuery(trade_after, product, id, limit)
rows, err := s.client.db.Query(trade_after, product, id, limit)
2017-09-09 10:14:50 -07:00
if err != nil {
2017-09-23 16:52:47 -07:00
s.client.logError(trade_after, err, product, id, limit)
2017-09-10 13:04:02 -07:00
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
}
2017-09-23 16:52:47 -07:00
func (s *TradeService) TradesBefore(product string, id, limit int) ([]tacitus.Trade, error) {
s.client.logQuery(trade_before, product, id, limit)
rows, err := s.client.db.Query(trade_before, product, id, limit)
2017-09-09 10:14:50 -07:00
if err != nil {
2017-09-23 16:52:47 -07:00
s.client.logError(trade_before, err, product, id, limit)
2017-09-10 13:04:02 -07:00
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-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
}