-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.go
executable file
·86 lines (69 loc) · 1.91 KB
/
asset.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
package history
import (
sq "github.com/Masterminds/squirrel"
"github.com/paydex-core/paydex-go/xdr"
)
func (q *Q) GetAssetByID(dest interface{}, id int64) (err error) {
sql := sq.Select("id", "asset_type", "asset_code", "asset_issuer").From("history_assets").Limit(1).Where(sq.Eq{"id": id})
err = q.Get(dest, sql)
return
}
// GetAssetIDs fetches the ids for many Assets at once
func (q *Q) GetAssetIDs(assets []xdr.Asset) ([]int64, error) {
list := make([]string, 0, len(assets))
for _, asset := range assets {
list = append(list, asset.String())
}
sql := sq.Select("id").From("history_assets").Where(sq.Eq{
"concat(asset_type, '/', asset_code, '/', asset_issuer)": list,
})
var ids []int64
err := q.Select(&ids, sql)
return ids, err
}
// GetAssetID fetches the id for an Asset. If fetching multiple values, look at GetAssetIDs
func (q *Q) GetAssetID(asset xdr.Asset) (id int64, err error) {
var (
assetType string
assetCode string
assetIssuer string
)
err = asset.Extract(&assetType, &assetCode, &assetIssuer)
if err != nil {
return
}
sql := sq.Select("id").From("history_assets").Limit(1).Where(sq.Eq{
"asset_type": assetType,
"asset_code": assetCode,
"asset_issuer": assetIssuer})
err = q.Get(&id, sql)
return
}
// Get asset row id. If asset is first seen, it will be inserted and the new id returned.
func (q *Q) GetCreateAssetID(
asset xdr.Asset,
) (result int64, err error) {
result, err = q.GetAssetID(asset)
//asset exists, return id
if err == nil {
return
}
//unexpected error
if !q.NoRows(err) {
return
}
//insert asset and return id
var (
assetType string
assetCode string
assetIssuer string
)
err = asset.Extract(&assetType, &assetCode, &assetIssuer)
if err != nil {
return
}
err = q.GetRaw(&result,
`INSERT INTO history_assets (asset_type, asset_code, asset_issuer) VALUES (?,?,?) RETURNING id`,
assetType, assetCode, assetIssuer)
return
}