-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
144 lines (117 loc) · 4.06 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package postgres
import (
"context"
"database/sql"
"time"
"github.com/jmoiron/sqlx"
q "github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/currency"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
)
const (
tableName = "codewallet__core_exchangerate"
dateFormat = "2006-01-02"
)
type model struct {
Id sql.NullInt64 `db:"id"`
ForDate string `db:"for_date"`
ForTimestamp time.Time `db:"for_timestamp"`
CurrencyCode string `db:"currency_code"`
CurrencyRate float64 `db:"currency_rate"`
}
func toModel(obj *currency.ExchangeRateRecord) *model {
return &model{
Id: sql.NullInt64{Int64: int64(obj.Id), Valid: obj.Id > 0},
ForDate: obj.Time.UTC().Format(dateFormat),
ForTimestamp: obj.Time.UTC(),
CurrencyCode: obj.Symbol,
CurrencyRate: obj.Rate,
}
}
func fromModel(obj *model) *currency.ExchangeRateRecord {
return ¤cy.ExchangeRateRecord{
Id: uint64(obj.Id.Int64),
Time: obj.ForTimestamp.UTC(),
Symbol: obj.CurrencyCode,
Rate: obj.CurrencyRate,
}
}
func makeInsertQuery() string {
return `INSERT INTO ` + tableName + ` (for_date, for_timestamp, currency_code, currency_rate)
VALUES ($1, $2, $3, $4) RETURNING *;`
}
func makeSelectQuery(condition string, ordering q.Ordering) string {
return `SELECT * FROM ` + tableName + ` WHERE ` + condition + ` ORDER BY for_timestamp ` + q.FromOrderingWithFallback(ordering, "asc")
}
func makeGetQuery(condition string, ordering q.Ordering) string {
return makeSelectQuery(condition, ordering) + ` LIMIT 1`
}
func makeRangeQuery(condition string, ordering q.Ordering, interval q.Interval) string {
var query, bucket string
if interval == q.IntervalRaw {
query = `SELECT *`
} else {
bucket = `date_trunc('` + q.FromIntervalWithFallback(interval, "hour") + `', for_timestamp)`
query = `SELECT DISTINCT ON (` + bucket + `) *`
}
query = query + ` FROM ` + tableName + ` WHERE ` + condition
if interval == q.IntervalRaw {
query = query + ` ORDER BY for_timestamp ` + q.FromOrderingWithFallback(ordering, "asc")
} else {
query = query + ` ORDER BY ` + bucket + `, for_timestamp DESC` // keep only the latest record for each bucket
}
//fmt.Printf("query: %s\n", query)
return query
}
func (self *model) txSave(ctx context.Context, tx *sqlx.Tx) error {
err := tx.QueryRowxContext(ctx,
makeInsertQuery(),
self.ForDate,
self.ForTimestamp,
self.CurrencyCode,
self.CurrencyRate,
).StructScan(self)
return pgutil.CheckUniqueViolation(err, currency.ErrExists)
}
func dbGetBySymbolAndTime(ctx context.Context, db *sqlx.DB, symbol string, t time.Time, ordering q.Ordering) (*model, error) {
res := &model{}
err := db.GetContext(ctx, res,
makeGetQuery("currency_code = $1 AND for_date = $2 AND for_timestamp <= $3", ordering),
symbol,
t.UTC().Format(dateFormat),
t.UTC(),
)
return res, pgutil.CheckNoRows(err, currency.ErrNotFound)
}
func dbGetAllByTime(ctx context.Context, db *sqlx.DB, t time.Time, ordering q.Ordering) ([]*model, error) {
query := `SELECT DISTINCT ON (currency_code) *
FROM ` + tableName + `
WHERE for_date = $1 AND for_timestamp <= $2
ORDER BY currency_code, for_timestamp ` + q.FromOrderingWithFallback(ordering, "asc")
res := []*model{}
err := db.SelectContext(ctx, &res, query, t.UTC().Format(dateFormat), t.UTC())
if err != nil {
return nil, pgutil.CheckNoRows(err, currency.ErrNotFound)
}
if res == nil {
return nil, currency.ErrNotFound
}
if len(res) == 0 {
return nil, currency.ErrNotFound
}
return res, nil
}
func dbGetAllForRange(ctx context.Context, db *sqlx.DB, symbol string, interval q.Interval, start time.Time, end time.Time, ordering q.Ordering) ([]*model, error) {
res := []*model{}
err := db.SelectContext(ctx, &res,
makeRangeQuery("currency_code = $1 AND for_timestamp >= $2 AND for_timestamp <= $3", ordering, interval),
symbol, start.UTC(), end.UTC(),
)
if err != nil {
return nil, pgutil.CheckNoRows(err, currency.ErrNotFound)
}
if len(res) == 0 {
return nil, currency.ErrNotFound
}
return res, nil
}