package postgres import ( "github.com/kcotugno/tacitus" ) const ( conf_columns = `product, last_trade_id` conf_insert = `INSERT INTO confirmations (` + conf_columns + `) VALUES ($1, $2) RETURNING id, ` + conf_columns + `;` conf_find_product = `SELECT id, ` + conf_columns + ` FROM confirmations` + `WHERE product = $1;` conf_update = `UPDATE confirmation SET last_trade_id = $1 WHERE product = $2;` ) type ConfirmationService struct { client *Client } func (s *ConfirmationService) Confirmation(product string) (tacitus.Confirmation, error) { var conf tacitus.Confirmation s.client.logQuery(conf_find_product, product) row := s.client.db.QueryRow(conf_find_product, product) err := row.Scan(&conf.Id, &conf.Product, &conf.LastTradeId) if err != nil { s.client.logError(conf_find_product, err, product) return conf, err } return conf, nil } func (s *ConfirmationService) CreateConfirmation(c tacitus.Confirmation) (tacitus.Confirmation, error) { var conf tacitus.Confirmation s.client.logQuery(conf_insert, c.Product, c.LastTradeId) row := s.client.db.QueryRow(conf_insert, c.Product, c.LastTradeId) err := row.Scan(&conf.Id, &conf.Product, &conf.LastTradeId) if err != nil { s.client.logError(conf_insert, err, c.Product, c.LastTradeId) return conf, err } return conf, nil } func (s *ConfirmationService) UpdateConfirmation(c tacitus.Confirmation) (tacitus.Confirmation, error) { s.client.logQuery(conf_insert, c.LastTradeId, c.Product) _, err := s.client.db.Exec(conf_update, c.LastTradeId, c.Product) if err != nil { s.client.logError(conf_update, err, c.LastTradeId, c.Product) } return c, err }