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/aggregation_results.go
2017-10-17 22:12:46 -07:00

90 lines
1.5 KiB
Go

package postgres
import (
"github.com/kcotugno/tacitus"
"github.com/shopspring/decimal"
"database/sql"
"errors"
"time"
)
type AggregationResults struct {
rows *sql.Rows
aggregation tacitus.Aggregation
err error
}
func (r *AggregationResults) Next() bool {
var a tacitus.Aggregation
var id, interval, buyTransactions, sellTransactions *int
var product *string
var price, buyVolume, sellVolume *decimal.Decimal
var timestamp *time.Time
if r.rows == nil {
r.err = errors.New("No query results")
return false
}
if r.rows.Next() {
r.err = r.rows.Scan(&id, &interval, &product, &price, &buyVolume,
&sellVolume, &buyTransactions, &sellTransactions, &timestamp)
if id != nil {
a.Id = *id
}
if interval != nil {
a.Interval = *interval
}
if product != nil {
a.Product = *product
}
if price != nil {
a.Price = *price
}
if buyVolume != nil {
a.BuyVolume = *buyVolume
}
if sellVolume != nil {
a.SellVolume = *sellVolume
}
if buyTransactions != nil {
a.BuyTransactions = *buyTransactions
}
if sellTransactions != nil {
a.SellTransactions = *sellTransactions
}
if timestamp != nil {
a.Timestamp = *timestamp
}
r.aggregation = a
} else {
r.err = r.rows.Err()
return false
}
if r.err == nil {
return true
} else {
return false
}
}
func (r *AggregationResults) Value() interface{} {
return r.aggregation
}
func (r *AggregationResults) Error() error {
return r.err
}
func (r *AggregationResults) Close() error {
if r.rows != nil {
return r.rows.Close()
}
return nil
}