-
Notifications
You must be signed in to change notification settings - Fork 19
/
accounts_filter.go
123 lines (102 loc) · 3.54 KB
/
accounts_filter.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package sqlstore
import (
"fmt"
"code.vegaprotocol.io/vega/datanode/entities"
)
// Return an SQL query string and corresponding bind arguments to return rows
// from the account table filtered according to this AccountFilter.
func filterAccountsQuery(af entities.AccountFilter, includeVegaTime bool) (string, []interface{}, error) {
var args []interface{}
var err error
query := `SELECT id, party_id, asset_id, market_id, type FROM ACCOUNTS `
if includeVegaTime {
query = `SELECT id, party_id, asset_id, market_id, type, vega_time FROM ACCOUNTS `
}
if af.AssetID.String() != "" {
query = fmt.Sprintf("%s WHERE asset_id=%s", query, nextBindVar(&args, af.AssetID))
} else {
query = fmt.Sprintf("%s WHERE true", query)
}
if len(af.PartyIDs) > 0 {
partyIDs := make([][]byte, len(af.PartyIDs))
for i, party := range af.PartyIDs {
partyIDs[i], err = party.Bytes()
if err != nil {
return "", nil, fmt.Errorf("invalid party id: %w", err)
}
}
query += " AND party_id=ANY(" + nextBindVar(&args, partyIDs) + ")"
}
if len(af.AccountTypes) > 0 {
query += " AND type=ANY(" + nextBindVar(&args, af.AccountTypes) + ")"
}
if len(af.MarketIDs) > 0 {
marketIds := make([][]byte, len(af.MarketIDs))
for i, market := range af.MarketIDs {
marketIds[i], err = market.Bytes()
if err != nil {
return "", nil, fmt.Errorf("invalid market id: %w", err)
}
}
query += " AND market_id=ANY(" + nextBindVar(&args, marketIds) + ")"
}
return query, args, nil
}
func filterAccountBalancesQuery(af entities.AccountFilter) (string, []interface{}, error) {
var args []interface{}
where := ""
and := ""
if len(af.AssetID.String()) != 0 {
where = fmt.Sprintf("ACCOUNTS.asset_id=%s", nextBindVar(&args, af.AssetID))
and = " AND "
}
if len(af.PartyIDs) > 0 {
partyIDs := make([][]byte, len(af.PartyIDs))
for i, party := range af.PartyIDs {
bytes, err := party.Bytes()
if err != nil {
return "", nil, fmt.Errorf("could not decode party ID: %w", err)
}
partyIDs[i] = bytes
}
where = fmt.Sprintf(`%s%sACCOUNTS.party_id=ANY(%s)`, where, and, nextBindVar(&args, partyIDs))
if and == "" {
and = " AND "
}
}
if len(af.AccountTypes) > 0 {
where = fmt.Sprintf(`%s%stype=ANY(%s)`, where, and, nextBindVar(&args, af.AccountTypes))
if and == "" {
and = " AND "
}
}
if len(af.MarketIDs) > 0 {
marketIDs := make([][]byte, len(af.MarketIDs))
for i, market := range af.MarketIDs {
bytes, err := market.Bytes()
if err != nil {
return "", nil, fmt.Errorf("could not decode market ID: %w", err)
}
marketIDs[i] = bytes
}
where = fmt.Sprintf(`%s%sACCOUNTS.market_id=ANY(%s)`, where, and, nextBindVar(&args, marketIDs))
}
query := `SELECT ACCOUNTS.id, ACCOUNTS.party_id, ACCOUNTS.asset_id, ACCOUNTS.market_id, ACCOUNTS.type,
current_balances.balance, current_balances.tx_hash, current_balances.vega_time
FROM ACCOUNTS JOIN current_balances ON ACCOUNTS.id = current_balances.account_id `
if where != "" {
query = fmt.Sprintf("%s WHERE %s", query, where)
}
return query, args, nil
}